Answer the question
In order to leave comments, you need to log in
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
def category(reguest, slug):
cat = Category.objects.get(slug=slug)
return render_to_response('category.html', {'cat': cat})
url(r'^category/(?P<slug>\d+)/$', views.category, name='category'),
def category(reguest):
cat = Category.objects.all()
return render_to_response('category.html', {'cat': cat})
url(r'^category/$', views.category, name='category'),
Answer the question
In order to leave comments, you need to log in
(?P<slug>\d+)
\d is only digits
For a slug, you need something like:(?P<slug>[-\w]+)
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'),
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question