Reputation: 145
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?
Upvotes: 3
Views: 1941
Reputation: 45575
The name of the parameter in your urls.py
is pk
:
url(r'^category/(?P<pk>[0-9]+)/$', views.category_detail, name='category'),
But you are trying to find url with parameter category_id
. Change it to:
{% url 'blog.views.category_detail' pk=category.pk %}
Or even omit the name of parameter:
{% url 'blog.views.category_detail' category.pk %}
And imho, as far as your url has a name
, it is better to find this url by it's name instead of view's name:
{% url 'category' category.pk %}
UPDATE: In the post_list.html
you use this code:
<a href="{% url 'category_detail' pk=category.pk %}/">{{ cat.title }}</a>
As far as I understand the category variable is named cat
. Yous should pass the correct variable to the {% url %}
tag:
<a href="{% url 'category_detail' pk=cat.pk %}">{{ cat.title }}</a>
To solve such problems once and forever consider to implement the get_absolute_url()
method in your Category
model. Use this method any time you need to point to the category:
<a href="{{ cat.get_absolute_url }}">{{ cat.title }}</a>
Upvotes: 1
Reputation: 51
Have a look at your variable names, you are using cat
to get the title and category
to get the pk. Maybe category
is undefined? This will result in an empty string inserted in the url tag and would lead to the received error message.
Upvotes: 1