Django URL pattern - template tags

69 views
Skip to first unread message

Petey

unread,
Sep 2, 2011, 1:33:29 PM9/2/11
to django...@googlegroups.com
 Hey

I've made a view which should generate url in form like this (example.com/type/id/slug : example.com/news/1/lorem-ipsum/)

To generate links I use:
<a href="/{{ n.type }}/{{ n.id}}/{{ n.slug }}">{{ n.title|safe}}</a>
It isn't the correct form for that as far as I know.

Could someone explain me how to use {% url %} tag in this situation? I dont quite understand this and I can't manage it to work :)

http://pastebin.com/mVU0iFXt

Petey

unread,
Sep 2, 2011, 3:46:55 PM9/2/11
to django...@googlegroups.com
Problem solved.
I was too tired to find my mistake earlier ;)

Tomas Neme

unread,
Sep 2, 2011, 9:28:05 PM9/2/11
to django...@googlegroups.com
I'll still answer this just in case. You should have the following in
your urls.py:

urlpatterns += (......,
url(r'(P<type>)\w+/(P<id>)\d+/(P<slug>)\[w-]+/', your_view,
name='yoururlname'), ....)

and then you can have in your template

<a href="{% url yoururlname type=n.type id=n.id slug=n.slug %}">{{
n.title }}</a>


I hope I didn't make any mistakes.

tomas


--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

Piotr S

unread,
Sep 2, 2011, 11:54:45 PM9/2/11
to django...@googlegroups.com
All correct :)


--
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.


Reply all
Reply to author
Forward
0 new messages