python - Django Error passing URL argument from Template to View: NoReverseMatch Reverse not found. 1 pattern(s) tried -
i trying pass configure url so:
/details/12345
template html:
<div class="row"> {% if article_list %} {% article in article_list %} <div> <h2>{{ article.title }}</h2> <p>{{ article.body }}</p> <p><a class="btn btn-default" href="{% url 'details' article.id %}" role="button">view details »</a></p> </div><!--/.col-xs-6.col-lg-4--> {% endfor %} {% endif %} </div><!--/row-->
urls.py (full):
django.conf import settings django.conf.urls import include, url django.conf.urls.static import static django.contrib import admin urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^$', 'news_readr.views.home', name='home'), url(r'^details/(?p<article_id>\d+)/$', 'news_readr.views.details', name='details'), ] + static(settings.static_url, document_root=settings.static_root) if settings.debug: urlpatterns += static(settings.static_url, document_root=settings.static_root) urlpatterns += static(settings.media_url, document_root=settings.media_root)
views.py:
from django.shortcuts import render .models import article # create views here. def home(request): title = "home" article_list = article.objects.all() article in article_list: print(article.id) context = { "title": title, "article_list": article_list, } return render(request, "home.html", context) def details(request, article_id = "1"): article = article.objects.get(id=article_id) return render(request, "details.html", {'article': article})
i getting error says:
noreversematch @ / reverse 'details' arguments '()' , keyword arguments '{}' not found. 1 pattern(s) tried: ['details/(?p<article_id>\\d+)/$']
i'm 1 week old @ django, , think there's wrong url named group config. please help! tia!
update: if remove url config , change to:
url(r'^details/$', 'news_readr.views.details', name='details'),
the error changes to:
reverse 'details' arguments '(1,)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['details/$']
so seems picking argument passed 1
in case. seems issue regular expression. tried expression out @ pythex, there, expression doesn't seem matching anything.
for url pattern
url(r'^details/(?p<article_id>\d+)/$', 'news_readr.views.details', name='details'),
the correct way use tag
{% url 'details' article.id %}
this because details
url pattern has group article_id
, have pass tag.
if have above url pattern, , {{ article.id}}
displays correctly in template, above template tag should not give error reverse 'details' arguments '()'
. suggests have not updated code, or have not restarted server after changing code.
if change url pattern
url(r'^details/$', 'news_readr.views.details', name='details')
then need remove article.id
url tag.
{% url 'details' %}
Comments
Post a Comment