Django将模板中的URL参数传递给视图:NoReverseMatch反向未找到。尝试了1种模式 [英] Django Error passing URL argument from Template to View: NoReverseMatch Reverse not found. 1 pattern(s) tried
问题描述
我正在尝试通过配置一个如下URL:
/ details / 12345
模板HTML:
div class =row>
{%if article_list%}
{article for article_list%}
< div>
< h2> {{article.title}}< / h2>
< p> {{article.body}}< / p>
< p>< a class =btn btn-defaulthref ={%url'details'article.id%}role =button>查看详细信息&一个>< / p为H.
< / div><! - /。col-xs-6.col-lg-4 - >
{%endfor%}
{%endif%}
< / div><! - / row - >
urls.py(full):
<$来自django.conf导入设置的$ p code $ d从django.conf.urls导入包含
来自django.conf.urls.static的url
从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)
如果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
from .models import Article
#在这里创建您的观点。
def home(request):
title =Home
article_list = Article.objects.all()
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})
我发现错误:
NoReverseMatch at /
使用参数'()'和关键字参数'{}'$反转'details' b $ b未找到。 1模式尝试:['details /(?P< article_id> \\d +)/ $']
我在Django有一个星期的时间,我认为我的网址命名组配置有问题。请帮忙! TIA!
更新:如果我删除了URL配置并将其更改为:
url(r'^ details / $','news_readr.views.details',name ='details'),
错误更改为:
使用参数'(1,)'和关键字参数'{}'
未找到。 1模式尝试:['details / $']
所以似乎正在拿起传递的参数<在这种情况下,code> 1 所以这似乎是正则表达式的一个问题。我在Pythex尝试了表达式,但即使在那里,表达式似乎并不匹配任何东西。
对于url模式
url(r'^ details /(?P< article_id> \d +)/ $','news_readr.views.details',name ='details'),
使用标签的正确方法是
{%url'details'article.id%}
这是因为
url pattern有一个组 article_id
,所以你必须把它传递给标签。
如果您有上述的URL格式,并且 {{article.id}} 在模板中正确显示,则上述模板标签不应该给出错误使用参数'()'
反转'details'。这表示您没有更新代码,或者您在更改代码后还没有重新启动服务器。
如果将URL格式更改为
url(r'^ details / $','news_readr.views.details',name ='details')
那么你需要从url标签中删除 article.id
。
{%url'details'%}
I am trying to pass configure a URL like so:
/details/12345
Template HTML:
<div class="row">
{% if article_list %}
{% for 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):
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from 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
from .models import Article
# Create your views here.
def home(request):
title = "Home"
article_list = Article.objects.all()
for 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 am getting an error that says:
NoReverseMatch at /
Reverse for 'details' with arguments '()' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/(?P<article_id>\\d+)/$']
I'm one week old at Django, and I think there's something wrong with my URL Named Group config. Please help! TIA!
Update: If I remove the URL config and change it back to:
url(r'^details/$', 'news_readr.views.details', name='details'),
The error changes to:
Reverse for 'details' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['details/$']
So it seems to be picking up the argument that is passed 1
in this case. So this seems to be an issue with the regular expression. I tried the expression out at Pythex, but even there, the expression doesn't seem to be matching anything.
For the url pattern
url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
The correct way to use the tag is
{% url 'details' article.id %}
This because the details
url pattern has a group article_id
, so you have to pass this to the tag.
If you have the above url pattern, and {{ article.id}}
displays correctly in the template, then the above template tag should not give the error Reverse for 'details' with arguments '()'
. That suggests you have not updated the code, or you have not restarted the server after changing code.
If you change the url pattern to
url(r'^details/$', 'news_readr.views.details', name='details')
then you need to remove the article.id
from the url tag.
{% url 'details' %}
这篇关于Django将模板中的URL参数传递给视图:NoReverseMatch反向未找到。尝试了1种模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!