Django url 有多个 slug,没有反向匹配错误
Django url with multiple slugs, no reverse match error
我在 Django 中有一个博客项目,我希望能够根据 post 的类型过滤我的 posts,例如 Travel posts,Programming post秒。因此,在我的模板中,我需要通过 post slug 和 type slug 过滤 post,但我得到:
NoReverseMatch at /
Reverse for 'getPost' with keyword arguments '{'categoryTypeSlug': '', 'postTitleSlug': ''}' not found. 1 pattern(s) tried: ['categories/(?P[\w\-]+)/(?P[\w\-]+)/$']
我的简化模板(getCatTypePosts.html):
{% block content %}
{% for post in posts %}
{% if post.show_in_posts %}
<a href="{% url 'getPost' categoryTypeSlug postTitleSlug %}">
<img src="{{ MEDIA.URL }} {{post.editedimage.url}}" alt="image-{{post.title}}"/>
{% endif %}
{% endfor %}
{% endblock content %}
我的models.py
class categoryType(models.Model):
title = models.CharField(max_length=200)
categoryTypeSlug = models.SlugField(unique=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "categoryTypes"
def save(self, *args, **kwargs):
self.categoryTypeSlug = slugify(self.title)
super(categoryType, self).save(*args, **kwargs)
class Post(models.Model):
title = models.CharField(max_length=200)
postTitleSlug = models.SlugField()
summary = models.CharField(max_length=500, default=True)
body = RichTextUploadingField()
pub_date = models.DateTimeField(default=timezone.now)
category = models.ManyToManyField('Category')
categoryType = models.ManyToManyField('categoryType')
author = models.ForeignKey(User, default=True)
authorSlug = models.SlugField()
editedimage = ProcessedImageField(upload_to="primary_images",
null=True,
processors = [Transpose()],
format="JPEG")
show_in_posts = models.BooleanField(default=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.postTitleSlug = slugify(self.title)
self.authorSlug = slugify(self.author)
super(Post, self).save(*args, **kwargs)
观看次数
def getCatTypePosts(request, categoryTypeSlug='Travel'):
posts = Post.objects.all()
posts = posts.filter(categoryType__title='Travel')
posts = posts.order_by('-pub_date')
context = {
'posts':posts,
}
return render(request, 'posts/getCatTypePosts.html', context)
def getPost(request, postTitleSlug, categoryTypeSlug):
post = Post.objects.all()
categoryTypeSlug =
post.filter(categoryType__categoryTypeSlug=categoryTypeSlug)
postTitleSlug = post.filter(post.postTitleSlug)
context = {
'post':post,
'categoryTypeSlug':categoryTypeSlug,
'postTitleSlug':postTitleSlug,
}
return render(request, 'posts/getPost.html', context)
URL 配置文件
urlpatterns = [
url(r'^$', views.getCatTypePosts, name='home'),
url(r'^categories/(?P<categoryTypeSlug>[\w\-]+)/(?P<postTitleSlug>
[\w\-]+)/$', views.getPost, name='getPost'),
url(r'^posts/(?P<categoryTypeSlug>[\w\-]+)/$',
views.getCatTypePosts, name = 'getCatTypePosts'),
]
非常感谢任何帮助。
您的错误发生在 /
,由 getCatTypePosts
处理。此视图不会将 categoryTypeSlug
和 postTitleSlug
添加到上下文中,因此您的 {% url %}
标记给出了一个错误,指出关键字参数是 ''
.
由于 {% url %}
标记位于 {% for post in posts %}
for 循环内,您可以使用 post.postTitleSlug
而不是 postTitleSlug
。如何替换 categoryType
不太明显,因为它是多对多字段 - 不清楚您想在那里使用什么值。您可能会使用 post.categoryType.first.categoryTypeSlug
,只要 每个 post 至少有一个相关类别。
我在 Django 中有一个博客项目,我希望能够根据 post 的类型过滤我的 posts,例如 Travel posts,Programming post秒。因此,在我的模板中,我需要通过 post slug 和 type slug 过滤 post,但我得到:
NoReverseMatch at / Reverse for 'getPost' with keyword arguments '{'categoryTypeSlug': '', 'postTitleSlug': ''}' not found. 1 pattern(s) tried: ['categories/(?P[\w\-]+)/(?P[\w\-]+)/$']
我的简化模板(getCatTypePosts.html):
{% block content %}
{% for post in posts %}
{% if post.show_in_posts %}
<a href="{% url 'getPost' categoryTypeSlug postTitleSlug %}">
<img src="{{ MEDIA.URL }} {{post.editedimage.url}}" alt="image-{{post.title}}"/>
{% endif %}
{% endfor %}
{% endblock content %}
我的models.py
class categoryType(models.Model):
title = models.CharField(max_length=200)
categoryTypeSlug = models.SlugField(unique=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "categoryTypes"
def save(self, *args, **kwargs):
self.categoryTypeSlug = slugify(self.title)
super(categoryType, self).save(*args, **kwargs)
class Post(models.Model):
title = models.CharField(max_length=200)
postTitleSlug = models.SlugField()
summary = models.CharField(max_length=500, default=True)
body = RichTextUploadingField()
pub_date = models.DateTimeField(default=timezone.now)
category = models.ManyToManyField('Category')
categoryType = models.ManyToManyField('categoryType')
author = models.ForeignKey(User, default=True)
authorSlug = models.SlugField()
editedimage = ProcessedImageField(upload_to="primary_images",
null=True,
processors = [Transpose()],
format="JPEG")
show_in_posts = models.BooleanField(default=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.postTitleSlug = slugify(self.title)
self.authorSlug = slugify(self.author)
super(Post, self).save(*args, **kwargs)
观看次数
def getCatTypePosts(request, categoryTypeSlug='Travel'):
posts = Post.objects.all()
posts = posts.filter(categoryType__title='Travel')
posts = posts.order_by('-pub_date')
context = {
'posts':posts,
}
return render(request, 'posts/getCatTypePosts.html', context)
def getPost(request, postTitleSlug, categoryTypeSlug):
post = Post.objects.all()
categoryTypeSlug =
post.filter(categoryType__categoryTypeSlug=categoryTypeSlug)
postTitleSlug = post.filter(post.postTitleSlug)
context = {
'post':post,
'categoryTypeSlug':categoryTypeSlug,
'postTitleSlug':postTitleSlug,
}
return render(request, 'posts/getPost.html', context)
URL 配置文件
urlpatterns = [
url(r'^$', views.getCatTypePosts, name='home'),
url(r'^categories/(?P<categoryTypeSlug>[\w\-]+)/(?P<postTitleSlug>
[\w\-]+)/$', views.getPost, name='getPost'),
url(r'^posts/(?P<categoryTypeSlug>[\w\-]+)/$',
views.getCatTypePosts, name = 'getCatTypePosts'),
]
非常感谢任何帮助。
您的错误发生在 /
,由 getCatTypePosts
处理。此视图不会将 categoryTypeSlug
和 postTitleSlug
添加到上下文中,因此您的 {% url %}
标记给出了一个错误,指出关键字参数是 ''
.
由于 {% url %}
标记位于 {% for post in posts %}
for 循环内,您可以使用 post.postTitleSlug
而不是 postTitleSlug
。如何替换 categoryType
不太明显,因为它是多对多字段 - 不清楚您想在那里使用什么值。您可能会使用 post.categoryType.first.categoryTypeSlug
,只要 每个 post 至少有一个相关类别。