I just need some feedback and advice about my project layout and design.

1 view
Skip to first unread message

chyea

unread,
Jul 27, 2009, 6:13:58 PM7/27/09
to Django users
Hi all,

I'm writing a very small, simple video game news media site. I'm
starting off small, with just Blurbs and Reviews. The Blurbs are
basically quick news articles. The Reviews are extensive articles
about a video game, ofcourse.

So, since both Blurbs and Reviews are both types of articles, I just
created an abstract base class for a model - my Article model. And
then I created a Blurb and Review model. These models live in an
application named "articles".

Now, with these two models the mission is simple - map some urls to
views that query the database for the model data and then render the
template. I really want my urls to look something like this...

site.com/review/
site.com/review/this-is-a-reviews-slug/

site.com/blurb/
site.com/blurb/this-is-a-blurbs-slug/

Since the view for both of these urls would be identical, if i made it
generic by passing in the model type, I decided to whip up a base
urls.py that looked like the following: http://dpaste.com/71150/

That base urls.py file includes the same url patterns for the slug and
index views, which exist inside the actual "articles" application, and
the included urls.py file looks like this: http://dpaste.com/71151/

The two views that are being used here accept the relevant model as a
parameter and use it to query the database and simply pass the results
onto a generic template for displaying the data. This all sounds very
nice and elegant. I wasn't duplicating parts of my code and everything
I had written was very simple. The problem came though when in my
template I was wanting to display a permalink, or just simply use a
reverse lookup to get the link of the article currently being viewed.

When using the {% url %} tag, the reverse lookup was for some odd
reason returning the "site.com/review/" pattern, which is the second
pattern, even if the article was a Blurb. I'm not sure why it'd choose
the second matching pattern instead of the first, but either way this
behavior is nothing something I was wanting to hack my way around. So,
I added in the namespaces and in my template tried to use them in the
reverse url lookup by doing the follow: {% url article:single
article.slug %}. This was still returning the "site.com/review/"
pattern match for some reason.

So, I ask - is there something I'm doing wrong? Am I not properly
using namespaces? They sound to me like they should correct this
situation but don't appear to be working properly. Is there a better
way to organize this site layout? I could easily just create two apps
- one for blurbs and one for reviews but they'd both contain identical
code, with simple a different template tag line. I could also double
the number of views and url patterns in my articles app, and point
them all to their own custom templates. Both of these options are much
less elegant, though.

Thanks for any advice in advance!

Alex Robbins

unread,
Jul 28, 2009, 9:24:35 AM7/28/09
to Django users
Sorry, I don't know anything about url namespaces, but this might
help:
If all you are doing is looking up an object and displaying it, I
would use the built-in generic views[1]. Just provide a queryset and
an object id or slug and slug field. Less code to debug and maintain.
There is also a list display for your root url[2] inside the app.

[1] http://docs.djangoproject.com/en/dev/ref/generic-views/#django-views-generic-list-detail-object-detail
[2] http://docs.djangoproject.com/en/dev/ref/generic-views/#django-views-generic-list-detail-object-list

Hope that helps,
Alex
Reply all
Reply to author
Forward
0 new messages