正在使用Django在一个简单的新闻网站上工作。我的问题是,当用户点击某个类别(例如体育)时,我希望能够在一个视图中的某个类别下列出某个类别的帖子...我在我的模型中创建了两个类,它们是:
class Category(models.Model):
POLITICS = 'Politics'
SPORTS = 'Sports'
ENTERTAINMENT = 'Entertainment'
TECHNOLOGY = 'Technology'
CHOICE_CATEGORY_TYPE = (
(POLITICS, 'Politics'),
(SPORTS, 'Sports'),
(ENTERTAINMENT, 'Entertainment'),
(TECHNOLOGY, 'Technology'),
)
category_type = models.CharField(max_length=50, choices=CHOICE_CATEGORY_TYPE, default=None)
def __str__(self):
return self.category_type
class Post(models.Model):
category_type = models.ForeignKey(Category, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
pub_date = models.DateField()
photo = models.ImageField()
body = models.TextField()
def __str__(self):
return self.title
其次,这是我的观点:
def index(request):
posts = Post.objects.all()
return render(request, 'blog/index.html', {'posts': posts})
def detail(request, pk):
article = get_object_or_404(Post, pk=pk)
return render(request, 'blog/detail.html', {'article': article})
def categories(request, category_type=None):
category_post = get_object_or_404(Category, category_type=category_type)
post_list = Post.objects.all()
return render(request, 'blog/category.html', {'category_post':
category_post, 'post_list': post_list})
最后我的网址:
from django.conf.urls import url
from . import views
app_name = 'blog'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.detail, name='detail'),
url(r'^categories/(?P<category_type>[0-9]+)/$', views.categories,
name='categories'),
]
最后,当我尝试在类别名称中创建超链接以引导我到该类别页面以从其索引页面获取其下的帖子列表时,这是我得到的错误。
Reverse for 'categories' with arguments '(<Category: Sports>,)' not found. 1
pattern(s) tried: ['blog/categories/(?P<category_type>[0-9]+)/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 1.11.5
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'categories' with arguments '(<Category: Sports>,)' not found. 1
pattern(s) tried: ['blog/categories/(?P<category_type>[0-9]+)/$']
Exception Location: C:\Users\Abdul-Waris\Desktop\website\venv\lib\site-
packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable: C:\Users\Abdul-
Waris\Desktop\website\venv\Scripts\python.exe
Python Version: 3.6.2
Python Path:
['C:\\Users\\Abdul-Waris\\Desktop\\website\\mysite',
'C:\\Users\\Abdul-Waris\\Desktop\\website\\venv\\Scripts\\python36.zip',
'C:\\Users\\Abdul-Waris\\Desktop\\website\\venv\\DLLs',
'C:\\Users\\Abdul-Waris\\Desktop\\website\\venv\\lib',
'C:\\Users\\Abdul-Waris\\Desktop\\website\\venv\\Scripts',
'c:\\users\\abdul-waris\\appdata\\local\\programs\\python\\python36\\Lib',
'c:\\users\\abdul-waris\\appdata\\local\\programs\\python\\python36\\DLLs',
'C:\\Users\\Abdul-Waris\\Desktop\\website\\venv',
'C:\\Users\\Abdul-Waris\\Desktop\\website\\venv\\lib\\site-packages']
Server time: Fri, 29 Sep 2017 22:23:48 +0000
谢谢
答案 0 :(得分:1)
错误的第一行是
Reverse for 'categories' with arguments '(<Category: Sports>,)' not found.
如果你查看你的urls.py,你就像这样定义了类别页面的网址:
url(r'^categories/(?P<category_type>[0-9]+)/$', views.categories, name='categories')
您用于端点的正则表达式接受1位或更多位数0-9。所以它会匹配像/ categories / 3或/ categories / 2632301这样的网址,但不匹配诸如“体育”之类的字母。我建议更改正则表达式以接受1个或更多的字母数字。此外,由于这是用于查看单个类别的URL,因此我不会将URL命名为“类别”,而是可能类似“category”或“one_category”:
url(r'^categories/(?P<category_type>[a-zA-Z0-9]+)/$', views.categories, name='category')
现在,您的网址将正确匹配以下网址,例如/ categories / sports,/ categories / entertainment和/ categories / SpoRts123
另外,在您的blog / detail.html模板中,您可以动态创建一个到帖子类别的URL,如下所示:
{% url 'category' category_type=article.category_type %}
我的最后建议是,将外键设为ManyToManyField的类别;想想,你可以有一个包含很多帖子的类别,一个帖子可以有很多类别。这取决于您的背景,完全取决于您,但我建议您使用多对多,因为您可能会决定将多个类别附加到帖子/文章中。