Sunday, March 31, 2013

Re: how do I specify optional params in url?

Al 31/03/13 21:55, En/na frocco ha escrit:
> I am kinda lost with url encoding being a newbie in django.
>
> I do not see my error.
>
> Can you give me an example?

Here it is:

views.py:

def my_view(request, foo, bar="spam"):
pass

urls.py

# choose descriptive names for the views
...
url(r"^narrow/(\d+)/$", views.my_view, name="narrow1"),
url(r"^narrow/(\d+)/(\w+)/$", views.my_view, name="narrow2"),
...

in python code (views etc.):

reverse("narrow1", args=(23, )) -> ".../narrow/23/"
reverse("narrow2", args=(23, "foo")) -> ".../narrow/23/foo"

in templates:

{% url "narrow1" 23 %} -> ".../narrow/23/"
{% url "narrow2" 23 "foo" %} -> ".../narrow/23/foo/"

Important: if you're using django < 1.5 you should remove the quotes
around the view name in the url tag.




HTH

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

No comments:

Post a Comment