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?
myapp:index?