On Wed, 2008-07-16 at 22:31 -0700, Justin Myers wrote:
> Hey there. I'm starting a term as web development editor at my student
> newspaper in the fall, and we recently (February) launched a new site
> created in Django. My background's definitely more in design than in
> development per se, so I'm trying to get up to speed with Django by
> putting together a blogging application for the site. Shouldn't be too
> hard--yay for generic views--but I'm having trouble with the URLconf
> at the moment.
> 
> The problem is that I need to be able to support multiple blogs (for
> example, one on student government, one on the local music scene,
> etc.), hence the Blog.slug field, which I'm hoping to be able to grab
> from the URL so I can figure out which entries I need to pass to the
> template. With that in mind, then, I've got the following URLconf
> (included in the root URLconf at /blog/):
> 
> ---
> 
> from django.conf.urls.defaults import *
> from maneater.blogging.models import Entry, Blog
> 
> urlpatterns = patterns('django.views.generic.list_detail',
>     (r'^$', 'object_list', {'queryset': Blog.objects.all(),}),
> )
> 
> urlpatterns += patterns('django.views.generic.date_based',
>     (r'^(?P<blog_slug>[-\w]+)/$', 'archive_index', {'queryset':
> Entry.objects.filter(blog__slug__exact=blog_slug), 'date_field':
> 'date',}),
>     (r'^(?P<blog_slug>[-\w]+)/(?P<year>\d{4})/$', 'archive_year',
> {'queryset': Entry.objects.filter(blog__slug__exact=blog_slug),
> 'date_field': 'date',}),
>     (r'^(?P<blog_slug>[-\w]+)/(?P<year>\d{4})/(?P<month>\w{1,2})/$',
> 'archive_month', {'queryset':
> Entry.objects.filter(blog__slug__exact=blog_slug), 'date_field':
> 'date', 'month_format': '%m',}),
>     (r'^(?P<blog_slug>[-\w]+)/(?P<year>\d{4})/(?P<month>\w{1,2})/(?
> P<day>\w{1,2})/$', 'archive_day', {'queryset':
> Entry.objects.filter(blog__slug__exact=blog_slug), 'date_field':
> 'date', 'month_format': '%m',}),
>     (r'^(?P<blog_slug>[-\w]+)/(?P<year>\d{4})/(?P<month>\w{1,2})/(?
> P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', {'queryset':
> Entry.objects.filter(blog__slug__exact=blog_slug), 'date_field':
> 'date', 'month_format': '%m',}),
> )
> 
> ---
> 
> The object_list view works great, as long as it's the only pattern in
> the URLconf. As soon as I add all the date-based stuff, though, I get
> a fun little 500:
> 
> ---
> 
> Environment:
> 
> Request Method: GET
> Request URL: http://127.0.0.1:8000/blog/
> Django Version: 0.97-pre-SVN-unknown
> Python Version: 2.5.2
> Installed Applications:
> ['django.contrib.admin',
>  'maneater.newspaper_content',
>  'maneater.newspaper_publishing',
>  'maneater.podcasting',
>  'maneater.newspaper_staff',
>  'maneater.website_management',
>  'maneater.blogging',
>  'django.contrib.flatpages',
>  'django.contrib.contenttypes',
>  'django.contrib.auth',
>  'django.contrib.contenttypes',
>  'django.contrib.sessions',
>  'django.contrib.sites',
>  'django.contrib.sitemaps']
> Installed Middleware:
> ('django.contrib.sessions.middleware.SessionMiddleware',
>  'django.contrib.auth.middleware.AuthenticationMiddleware',
>  'django.middleware.gzip.GZipMiddleware',
>  'django.middleware.common.CommonMiddleware',
>  'django.middleware.doc.XViewMiddleware',
>  'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')
> 
> 
> Traceback:
> File "C:\Python25\Lib\site-packages\django\core\handlers\base.py" in
> get_response
>   76.             callback, callback_args, callback_kwargs =
> resolver.resolve(request.path)
> File "C:\Python25\Lib\site-packages\django\core\urlresolvers.py" in
> resolve
>   233.                     sub_match = pattern.resolve(new_path)
> File "C:\Python25\Lib\site-packages\django\core\urlresolvers.py" in
> resolve
>   231.             for pattern in self.urlconf_module.urlpatterns:
> File "C:\Python25\Lib\site-packages\django\core\urlresolvers.py" in
> _get_urlconf_module
>   255.                 raise ImproperlyConfigured, "Error while
> importing URLconf %r: %s" % (self.urlconf_name, e)
> 
> Exception Type: ImproperlyConfigured at /blog/
> Exception Value: Error while importing URLconf
> 'maneater.blogging.urls': name 'blog_slug' is not defined
> 
> ---
> 
> So I know it doesn't like how I'm trying to filter by the blog slug in
> the URL (capturing the named group <blog_slug> and trying to pass
> {'queryset': Entry.objects.filter(blog__slug__exact=blog_slug)} to the
> view). I'm not really sure how I _should_ go about it, though. Any
> ideas?

You are making the assumption that the queryset will be recreated anew
each time and that this will only happen after the blog_slug value has
been determined. Both assumptions aren't correct and this is what is
causing the error. The Python dictionary containing the queryset is
being evaluated as soon as that file is imported (and only then) and the
requisite information to create the queryset doesn't exist at that point
in time.

So, instead of trying to do this directly, you need to create the
queryset properly and then call the generic view. Do this by writing a
Python function that wraps up the call to the generic view. An example
can be found here:
http://www.pointy-stick.com/blog/2006/06/29/django-tips-extending-generic-views/

That's almost two years old now, but the logic (and even most of the
code) is still valid.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to