问题描述
我是django的新手。我遇到下面的错误消息异常值。
使用参数'()'和关键字'category_detail'没有找到参数'{u'pk':''}'。尝试一种模式:['category /(?P< pk> [0-9] +)/ $']
在模板渲染时出错
使用参数'()'和关键字参数'' {u'pk':''}'没有找到。尝试一种模式:['category /(?P< pk> [0-9] +)/ $']
行:
< li>< a href ={%url'category_detail' pk = category.pk%} /> {{cat.title}}< / a>< / li>
djangogirls / apsi / urls.py
urlpatterns = patterns('',
url(r'^ admin /',include(admin.site.urls)),
url(r include('blog.urls')),
)
djangogirls / blog / urls。从
from django.conf.urls import patterns,include,url
pre>
from。导入视图
urlpatterns = Patterns('',
url(r'^ post / $',views.post_list ),
url(r'^ category /(?P< pk> [0-9] +)/ $',views.category_detail,name ='category_detail'),
url(r' post /(?P< pk> [0-9] +)/ $',views.post_detail),
url(r'^ post / new / $',views.post_new,name ='post_new' ,
url(r'^ post /(?P< pk> [0-9] +)/ edit / $',views.post_edit,name ='post_edit'),
)
djangogirls / blog / views.py
def category_detail(request,pk):
category = Post.objects.filter(category__id = pk)
return render(request,'blog / category_detail.html ',{'category':category})
def post_list(request):
posts = Post.objects.filter(published_date__isnull = False).order_by('published_date' )
categories = Category.objects.all()
tags = Tag.objects.all()
ret urn渲染(请求,'blog / post_list.html',{'posts':posts,'categories':categories,'tags':tags})
djangogirls / blog / models.py
类类别(models.Model) :
title = models.CharField(max_length = 50)
slug = models.CharField(max_length = 200)
description = models.TextField()
created_date = models.DateTimeField(auto_now_add = True)
published_date = models.DateTimeField(blank = True,null = True)
def publish(self):
self.published_date = timezone。现在()
self.save()
def __str __(self):
return self.title
class Post(models.Model)
author = models.ForeignKey('auth.User')
category = models.ForeignKey('Category')
title = models.CharField(max_length = 200)
text = models.TextField()
c reated_date = models.DateTimeField(auto_now_add = True)
published_date = models.DateTimeField(blank = True,null = True)
likes = models.IntegerField(default = 0)
thumbnail = models。 FileField(upload_to = get_upload_file_name)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str __(self):
return self.title
你能帮我解决这个问题吗
解决方案看看你的变量名,你使用
cat
获得标题和类别
以获取pk。可能类别
未定义?这将导致在URL标签中插入一个空字符串,并导致收到的错误消息。i am newbie in django.I was encountered below error message exception value.
Reverse for 'category_detail' with arguments '()' and keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['category/(?P<pk>[0-9]+)/$']
error during template rendering
Reverse for 'category_detail' with arguments '()' and keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['category/(?P<pk>[0-9]+)/$']
in line:
<li><a href="{% url 'category_detail' pk=category.pk %}/">{{ cat.title }}</a></li>
djangogirls/apsi/urls.py
urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), url(r'', include('blog.urls')), )
djangogirls/blog/urls.py
from django.conf.urls import patterns, include, url
from . import views
urlpatterns = patterns('', url(r'^post/$', views.post_list), url(r'^category/(?P<pk>[0-9]+)/$', views.category_detail, name='category_detail'), url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail), url(r'^post/new/$', views.post_new, name='post_new'), url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'), )
djangogirls/blog/views.py
def category_detail(request, pk): category = Post.objects.filter(category__id=pk) return render(request, 'blog/category_detail.html', {'category': category}) def post_list(request): posts = Post.objects.filter(published_date__isnull=False).order_by('published_date') categories = Category.objects.all() tags = Tag.objects.all() return render(request, 'blog/post_list.html', {'posts': posts, 'categories': categories, 'tags':tags })
djangogirls/blog/models.py
class Category(models.Model): title = models.CharField(max_length=50) slug = models.CharField(max_length=200) description = models.TextField() created_date = models.DateTimeField(auto_now_add=True) published_date = models.DateTimeField(blank=True, null=True) def publish(self): self.published_date = timezone.now() self.save() def __str__(self): return self.title class Post(models.Model): author = models.ForeignKey('auth.User') category = models.ForeignKey('Category') title = models.CharField(max_length=200) text = models.TextField() created_date = models.DateTimeField(auto_now_add=True) published_date = models.DateTimeField(blank=True, null=True) likes = models.IntegerField(default=0) thumbnail = models.FileField(upload_to = get_upload_file_name) def publish(self): self.published_date = timezone.now() self.save() def __str__(self): return self.title
can you help me solve this problem?
解决方案Have a look at your variable names, you are using
cat
to get the title andcategory
to get the pk. Maybecategory
is undefined? This will result in an empty string inserted in the url tag and would lead to the received error message.这篇关于Django 1.7错误NoReverseMatch at'/ post /'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!