Django 类别及其子类别和 url
我正在尝试创建类别和子类别,我正在检查此模型 但我有这个错误:
Truncated incorrect DOUBLE value: 'nacionales'
其中“nacionales”是父类别,我知道我的问题可能在 urls.py 中,但事实是,我不知道如何设置这种情况下的 url...
我的 model.py :
# from ...
class Categoria(models.Model):
titulo = models.CharField(max_length=75, unique=True)
slug = models.SlugField(max_length=200,unique=True)
parent = models.ForeignKey('self', blank=True, null=True, related_name='child')
# functions....
我的观点.py:
# from ...
def noticias_categoria(request,parent_id,child):
categoria = get_object_or_404(Categoria,parent=parent_id,slug=child)
return object_list(request, queryset=categoria.noticia_set.all(), paginate_by=20,
template_name='categorias/categoria_list.html',
extra_context={'categoria':categoria})
我的类别 urls.py:
# from ...
url(r'^(?P<parent_id>[-\w]+)/(?P<child>[-\w]+)/$',
noticias_categoria,
name='noticia_detail'
),
我的 url.py:
(r'^categorias/', include('categorias.urls')),
谢谢大家
im trying to make Categories and Subcategories, im checking this models
but i have this error:
Truncated incorrect DOUBLE value: 'nacionales'
where the "nacionales" is the parent Category, i know that my problem maybe are in the urls.py, but the true, i dont know how set the urls for this case...
my model.py:
# from ...
class Categoria(models.Model):
titulo = models.CharField(max_length=75, unique=True)
slug = models.SlugField(max_length=200,unique=True)
parent = models.ForeignKey('self', blank=True, null=True, related_name='child')
# functions....
my views.py:
# from ...
def noticias_categoria(request,parent_id,child):
categoria = get_object_or_404(Categoria,parent=parent_id,slug=child)
return object_list(request, queryset=categoria.noticia_set.all(), paginate_by=20,
template_name='categorias/categoria_list.html',
extra_context={'categoria':categoria})
my Category urls.py:
# from ...
url(r'^(?P<parent_id>[-\w]+)/(?P<child>[-\w]+)/
my url.py:
(r'^categorias/', include('categorias.urls')),
Thanks guys
,
noticias_categoria,
name='noticia_detail'
),
my url.py:
Thanks guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在传递父类别 - nacionales - 的字符串值,其中函数需要数字 ID。
使用
parent_id
的 ID,或者将其重命名为parent
并编写函数的第一行,如下所示:It seems like you're passing in the string value of the parent category - nacionales - where the function is expecting the numeric ID.
Either use an ID for
parent_id
, or rename it toparent
and write the first line of the function like this: