Smarter URLs

0 views
Skip to first unread message

Rodion Raskolnikiv

unread,
Jul 7, 2010, 12:32:44 PM7/7/10
to Django users
Greetings!
I am trying to implement a very simple (yet elegant) solution for a
university departmental website in django. In designing my urls, I
desired to have them follow this pattern:
university.edu/department/page_title_made_into_slug

However, I couldn't get it working or find any doc that pointed out
how to do it, so I temporarily settled for:
univeristy.edu/1/14 (where 1 is the department ID and 14 is the
page ID)


This is how I have my URLs set up right now:
(r'^$', project.app.views.index'),
(r'^(?P<unit_id>\d+)/$', 'project.app.views.unit_page'),

I have not actually set the pages to display yet, I thought that I
would ask here before I did that ...


Could anyone direct me to a solution so that I could have my URLs the
way that I want them?

Daniel Lathrop

unread,
Jul 7, 2010, 12:54:45 PM7/7/10
to django...@googlegroups.com
I'm not quite clear on what you're asking, but if the issue is how to write a regex for slugs: The regular expression you need for slugs is [\w\d\-]+

Hope that helps.

Daniel
---------------------------
Daniel Lathrop




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


Rodion Raskolnikiv

unread,
Jul 7, 2010, 2:13:46 PM7/7/10
to Django users
To clarify, I would like to take the current url form of:

/dept_id/page_id/ (which looks like /2/13/)

into a url that looks like this:
/accounting/policy_for_travel_expenses/ (which puts the title of the
element in place of the element ID)

I am getting at the elements by ID this way:
(r'^(?P<unit_id>\d+)/$', 'project.app.views.unit_index'),
(r'^(?P<unit_id>\d+)/(?P<page_id>\d+)/$',
'project.app.views.unit_page'),

I am not sure how *[\w\d\-]+* would fit into my url configuration, I
am still learning the very basics.


On Jul 7, 9:54 am, Daniel Lathrop <daniel.lath...@gmail.com> wrote:
> I'm not quite clear on what you're asking, but if the issue is how to write
> a regex for slugs: The regular expression you need for slugs is *[\w\d\-]+*
>
> Hope that helps.
>
> Daniel
> ---------------------------
> Daniel Lathrop
>
> On Wed, Jul 7, 2010 at 9:32 AM, Rodion Raskolnikiv <noah...@gmail.com>wrote:
>
>
>
> > Greetings!
> > I am trying to implement a very simple (yet elegant) solution for a
> > university departmental website in django. In designing my urls, I
> > desired to have them follow this pattern:
> >    university.edu/department/page_title_made_into_slug
>
> > However, I couldn't get it working or find any doc that pointed out
> > how to do it, so I temporarily settled for:
> >    univeristy.edu/1/14 (where 1 is the department ID and 14 is the
> > page ID)
>
> > This is how I have my URLs set up right now:
> >    (r'^$', project.app.views.index'),
> >    (r'^(?P<unit_id>\d+)/$', 'project.app.views.unit_page'),
>
> > I have not actually set the pages to display yet, I thought that I
> > would ask here before I did that ...
>
> > Could anyone direct me to a solution so that I could have my URLs the
> > way that I want them?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users...@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > .

ringemup

unread,
Jul 7, 2010, 2:36:16 PM7/7/10
to Django users
Add a SlugField to the Page model, and populate it based on the title
(this can be done automatically either via the admin's
prepopulated_fields, or via a custom model save() method)

class Page(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField()

Then use a regex of the following form to pull the slug into your
view:

(r'^(?P<unit_id>\d+)/(?P<page_slug>[\w-]+)/$',
'project.app.views.unit_page'),

Then in your view:

def unit_page(request, unit_id, page_slug):
the_page = Page.objects.get(slug=PageSlug)

Do the same for the Unit model and regex matching. Adjust as
necessary to fit your actual models.

Rodion Raskolnikiv

unread,
Jul 7, 2010, 7:16:42 PM7/7/10
to Django users
Many thanks to you both!

That was enough info to get things working!
Reply all
Reply to author
Forward
0 new messages