0

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)),

]
4
  • 4
    The syntax is not deprecated. Only the 'path.to.some_view' dotted path syntax to specify the view is deprecated.
    – Martijn Pieters
    Oct 2, 2015 at 9:53
  • 1
    You'll need to show how you are using it exactly, the syntax as documented works just fine.
    – Martijn Pieters
    Oct 2, 2015 at 9:54
  • Please post the full traceback of 'str' object has no attribute 'regex'.
    – Alasdair
    Oct 2, 2015 at 15:10
  • Thanks all!, Today morning I restarted and its working fine!, I still didn't figure out why I was getting rendering error !! Anyway since Wtower helped me to understand it better, I'm going to accept his ans Oct 5, 2015 at 6:25

1 Answer 1

2

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 %}
5
  • 1
    Hi, Thanks!, I updated the question with code sections. Can you please tell me what exactly is going wrong? Oct 2, 2015 at 11:50
  • Welcome! Have you gat a namespace specified in your main urls.py? (Not the urls.py of your app).
    – Wtower
    Oct 2, 2015 at 12:46
  • ya, i have the namespace like this : url(r'^polls/', include('polls.urls', namespace = 'polls')), also noted that same error happens with "{% url 'polls:detail' que.id %}" Anyway I dont have multiple apps with same name for url Oct 2, 2015 at 13:00
  • Then {% url 'detail' que.id %} should be {% url 'polls:detail' que.id %}.
    – Wtower
    Oct 2, 2015 at 13:02
  • Even though i don't have any other url with the same name assigned, I checked {% url 'polls:detail' que.id %}. Same error during template rendering.. Oct 2, 2015 at 13:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.