Tuesday, July 19, 2011

Django urls, best practice

First iteration:
template html:
<a href="/msw/{{ page.slug }}/">back</a><



Second iteration:
template html:
<a href="{{ url('detail', 'richtext_and_safe_url') }}">back</a>
with
urls.py:
url(r'^(?P<input_slug>\w+)/$', views.detail, name='detail')


Third (best) iteration
template html:
<h1><a href="{{ page.reverse_url() }}">back</a></h1>
in models.py:
class Page(models.Model):
    slug = models.SlugField() # another way
    ....
    def reverse:_url(self):
        return reverse('detail', args=[self.slug])
        #return reverse('detail', args=['richtext_and_safe_url']) # hard coded way
be sure to pass in "page" in your views.py

inspiration

No comments:

Post a Comment