Dynamic URL

5 views
Skip to first unread message

djandrow

unread,
Aug 27, 2008, 7:45:19 PM8/27/08
to Django users
I'm trying to create a situation where you can bring up all the blog
entries in a category through the URL. I've got this in my urls.py:

from django.conf.urls.defaults import *
from akonline.views import current_datetime

urlpatterns = patterns('',

(r'^test/$', 'address.blog.views.blog'),
(r'^blog/(?P<category>\w+)/$',
'address.blog.views.category_entry'),
)

-------------------------------------------------------------------------------------------------------------

then this is the category_entry in my views:

def category_entry(request, category):
entries_in_cat = Entry.objects.filter(entry_cat=category)
return render_to_response('blog/index.html', locals())

-------------------------------------------------------------------------------------------------------------

then this is my pretty simple template:

<html><body>
{% for object in entries_in_cat %}
{{ object.entry_title }}<br /
{% endfor %}
</body></html>

-------------------------------------------------------------------------------------------------------------

then these models:

from django.db import models

class Category(models.Model):
cat_id = models.AutoField(primary_key=True)
cat_name = models.CharField(max_length=20)
cat_desc = models.CharField(max_length=200)

def __unicode__(self):
return self.cat_name

class Entry(models.Model):

entry_id = models.AutoField(primary_key=True)
entry_date = models.DateField(auto_now_add=True)
entry_title = models.CharField(max_length=70)
entry_content = models.TextField(max_length=5000)
entry_cat = models.ManyToManyField(Category)

def __unicode__(self):
return self.entry_title

-------------------------------------------------------------------------------------------------------------

I don't get an error, its just it returns a blank page, so i'd guess
either category isn't being read in correctly or the filter statement
is incorrect. I'd appericiate any help

Thanks,

Andrew

lingrlongr

unread,
Aug 27, 2008, 8:05:58 PM8/27/08
to Django users
Your problem is here:

def category_entry(request, category):
entries_in_cat = Entry.objects.filter(entry_cat=category)
return render_to_response('blog/index.html', locals())

Remember, the value of "category" is a string. One way or another you
need adjust the filter. Something like this should do the trick.

entries_in_cat = Entry.objects.filter(entry_cat__name=category)

-or-

thecat = Category.objects.get(cat_name=category)
entries_in_cat = Entry.objects.filter(entry_cat=thecat)

I prefer the first...

HTH

Keith

lingrlongr

unread,
Aug 27, 2008, 8:11:07 PM8/27/08
to Django users
Also, convention seems to be to spell out the field names. Because of
a different namespaces, you shouldn't have to worry about too much
name clashing. And if you don't provide an ID for your model, one is
automatically created for you. For example:

class Category(models.Model):
name = models.CharField(max_length=20)
description = models.CharField(max_length=200)

def __unicode__(self):
return u'%s' % self.name


On Aug 27, 7:45 pm, djandrow <AndrewKenyon...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages