How to create url for generic template without view?
I'm using {% url path_to_view %}
to generate links within my application. I came to the situation when I need to use several static pages without views, using direct_to_template
. Normally, if I had just one such page, I could use {% url direct_to_template %}
to generate link to it, but with more URLs pointing to this view, {% url direct_to_template %}
always points to the last one in urls.py
.
Is it possible to use {% url %}
syntax to point directly to some template? Or is the only choice to use 开发者_StackOverflow社区views even if I don't really need them?
Use named urls. if you have
url(regex=r'path/$', view=view_name, name='my_url'),
they you can use {% url my_url %}
to get it.
If you have django 1.3 use the class bases generic views, and you should create at least a url pattern
url.py
from django.views.generic import TemplateView
urlpatterns = patterns('',
url(r'^path/to/something/$', TemplateView.as_view(template_name="your_temlate.html"), name="something" ),
url(r'^path/to/something/else/$', TemplateView.as_view(template_name="your_temlate_else.html"), name="something_else" ),
)
template:
<a href="{% url something %}">Link to something</a>
<a href="{% url something_else %}">Link to something else</a>
You could use TemplateView
url(r'^path_to_html/$', TemplateView.as_view(template_name="path_to_html.html"), name='path_to_html'),
精彩评论