0

I designed this poll app and i'm trying convert a hard code url into an namespace url but had errors along the path.

This is my index.html and as you can see their an hard coded url that pointing to my URLconf.

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

My myapp URLconf.

 from django.conf.urls import patterns, include, url
 from django.contrib import admin
 from django.conf import settings

 from django.conf.urls import patterns, include, url

 urlpatterns = patterns('myapp.views',
     url(r'^$', 'index', name="index"),
     url(r'^(?P<poll_id>\d+)/$', 'detail',name="detail"),
     url(r'^(?P<poll_id>\d+)/results/$', 'results', name="results"),
     url(r'^(?P<poll_id>\d+)/vote/$', 'vote', name="vote"),
 )

This is my main URLconf.

 from django.conf.urls import patterns, include, url
 from django.contrib import admin
 from django.conf import settings

 admin.autodiscover()
 urlpatterns = patterns('',
     url(r'^polls/', include('myapp.urls', namespace='myapp')),                   
 ,
 )

My views are :

def detail(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('myapp/detail.html', {'poll': p},
context_instance=RequestContext(request))

I tried to replace the hard coded error with {% url detail poll.id %} or {% url myapp:detail poll.id %}

but i received this error

 NoReverseMatch at /polls/

 Reverse for 'detail' with arguments '(5,)' and keyword arguments '{}' not found.

 Request Method:    GET
 Request URL:   http://127.0.0.1:8000/polls/
 Django Version:    1.4.3

 Exception Type:    NoReverseMatch
  Exception Value:  

 Reverse for 'detail' with arguments '(5,)' and keyword arguments '{}' not found.

  Error during template rendering

  In template C:\djcode\mysite\myapp\templates\myapp\index.html, error at line 4
  Reverse for 'detail' with arguments '(5,)' and keyword arguments '{}' not found.
  1     {% if latest_poll_list %}
  2     <ul>
  3     {% for poll in latest_poll_list %}
  4     <li><a href="{% url detail poll.id %}">{{ poll.question }}</a></li>
  5     {% endfor %}
  6     </ul>
  7     {% else %}
  8     <p>No polls are available.</p>
  9     {% endif %}

How can I convert this hardcoded URL into an namespace so It could point to myapp URLconf without any errors?

3
  • I'm assuming you want the syntax myapp:index? Commented Feb 22, 2013 at 10:52
  • Well this index.html template passes a poll.id to the view function called detail . Commented Feb 22, 2013 at 10:55
  • The whole code is working fine , I just want to convert the hardcoded url into an namespace. "/polls/{{ poll.id }}/" points to detail function with the poll.id Commented Feb 22, 2013 at 10:56

1 Answer 1

0

The way of doing it is different depending on use-case.

You can either do, which probably is your usecase.

{% url detail poll.id %}

Where the url-tag matches on the name detail and the following poll.id

The other way we would like to discuss here is if you're going to have multiple instances of the same app then you would have to use url namespaces. Discussed here

Main urls.py

url(r'^polls/', include('core.urls')),

Included urls.py

urlpatterns = patterns('',
    url(r'^/(?P<polls_id>\d+)/$', 'core.views.polls_detail_view',name="poll_detail")
)

Template

 <a href="{% url poll_detail 1 %}">To Poll Detail</a>

`views.py``

def polls_detail_view(request, poll_detail):
     print "Hello Poll detail: %s" % (poll_detail)

** EDIT ** After googling around these two SO posts explains why OP's configuration is a no go.

First SO Post

Second SO Post

These are tickets from 19 months ago.

** EDIT **

Sign up to request clarification or add additional context in comments.

8 Comments

NoReverseMatch at /polls/ Reverse for 'detail' with arguments '(5,)' and keyword arguments '{}' not found.
I tried {% url myapp:detail poll.id %} too but received the same error
You most likely have something catching it higher up in your myapp url-conf. I'll post code that I made that works. 2 sec.
Yeah ThaNk You limelights@
Can I ask you . Did you test the poll_detail answer? because I got this error NoReverseMatch at /polls/ Reverse for 'poll_detail' with arguments '(1,)' and keyword arguments '{}' not found.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.