Shree Harsha S
Shree Harsha S

Reputation: 685

Alternative/ How to use {% url %} in django 1.8?

I'm learning django on version 1.8.

In their documentation they suggest to use {% url %} template tag in order to avoid hardcoding the url. But it is not working in the v1.8, and confirmed that it is deprecated in this version.

https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#url

Does anyone know any alternative ?

Update: p_index.html

{% if latest_question_list %}
    <ul>
    {% for que in latest_question_list %}
    <li><a href="{% url 'polls:detail' que.id %}">{{ que.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p> No polls questions are availabe. </p>
{% endif %}

Above code gives me this error: 'str' object has no attribute 'regex'

When I change the href line as below, works fine!

<li><a href="/polls/{{ que.id }}/">{{ que.question_text }}</a></li>

urlpatterns in polls app:

urlpatterns = [

    url(r'^$', views.index, name='view_index'),

    # ex: /polls/5/
    url(r'^(?P<q_no>[0-9]+)/$', views.detail, name='detail'),
    # ex: /polls/5/results/
    url(r'^(?P<q_no>[0-9]+)/results/$', views.results, name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<q_no>[0-9]+)/vote/$', views.vote, name='vote'),

]

The main urls.py is as below :

urlpatterns = [

    url(r'^$', 'home.views.index'),
    url(r'^polls/', include('polls.urls', namespace = 'polls')),
    url(r'^android/start', 'testpage.views.androidStart'),
    url(r'^admin/', include(admin.site.urls)),

]

Upvotes: 0

Views: 635

Answers (1)

Wtower
Wtower

Reputation: 19912

I think you got this wrong. The {% url %} tag is not depercated to my best of knowledge, and I have never heard of any alternative. What is deprecated is the syntax to pass a dotted Python path:

Deprecated since version 1.8:

You can also pass a dotted Python path to a view function, but this syntax is deprecated and will be removed in Django 1.10:

{% url 'path.to.some_view' v1 v2 %}

Upvotes: 2

Related Questions