B
B
blazer052015-12-22 10:36:50
Django
blazer05, 2015-12-22 10:36:50

Can't display categories in template?

Hello.
I'm completely confused in three pines - help me figure it out. I can't display categories with the slug parameter in the template. The browser gives an error page not found 404. But if you do it without a slug, but simply select from the database of all category entries, then everything works!
model

class Category(models.Model):
    class Meta():
        db_table = 'category'
    name = models.CharField(max_length=150, unique=True, verbose_name='Категория')
    slug = models.SlugField(verbose_name='Транслит', null=True)
    def __unicode__(self):
        return self.name

views.py
def category(reguest, slug):
    cat = Category.objects.get(slug=slug)
    return render_to_response('category.html', {'cat': cat})

urls.py
url(r'^category/(?P<slug>\d+)/$', views.category, name='category'),

If I rewrite without slug it works.
views.py
def category(reguest):
    cat = Category.objects.all()
    return render_to_response('category.html', {'cat': cat})

urls.py
url(r'^category/$', views.category, name='category'),

I noticed that he writes Raised by: blog.views.full_slug, it turns out that Django does not see the required url and takes the next one that stands behind it. I tried to put the url above and below but nothing!
What am I doing wrong?
2f56421e5bed4deaaf3518aaef0a3675.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Yablochkin, 2015-12-22
@Bers

(?P<slug>\d+)
\d is only digits
For a slug, you need something like:
(?P<slug>[-\w]+)

S
Sergey, 2015-12-22
@bogdanov-s

I have it done like this

url(r'^post/(?P<id>\d+)/$',views.get_post, name='get_post'),
url(r'^(?P<slug>[-\w\d]+)/(?:(?P<page>\d+)/)?$',views.get_section, name='get_secttion'),

The order is important, also in settings.py add APPEND_SLASH = True
I'm not sure, but I think that's the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question