[英]Django Reverse Error: NoReverseMatch at
Hello StackOverFlow Members, before i get down to the point, let me retrace my thought/process here to help further slim down my issue. 您好StackOverFlow成员们,在我深入探讨之前,让我在这里追溯我的想法/过程,以帮助进一步解决我的问题。 When i click a location object in "location_tree.html" it would redirect me to a new page, "location.html", displaying the location name and its type.
当我单击“ location_tree.html”中的位置对象时,它将重定向到新页面“ location.html”,显示位置名称及其类型。 From the very same page, the name would be a hyperlink to another page with more details about the "location".
在同一页面上,该名称将是到另一个页面的超链接,其中包含有关“位置”的更多详细信息。
Above is the general flow i want, but when i attempt to click the name from location.html, it redirects me to this error: 上面是我想要的一般流程,但是当我尝试单击location.html中的名称时,它会将我重定向到此错误:
NoReverseMatch at /accounts/location/2/ Reverse for 'continent' with keyword arguments '{u'pk': 2}' not found.
找不到/ accounts / location / 2 /上的NoReverseMatch,反向搜索带有关键字参数'{u'pk':2}'的'continent'。 1 >pattern(s) tried: ['accounts/location/(?>P\\d+)/location_continent/(?P\\d+)/']
1>尝试过的模式:['帐户/位置/(?> P \\ d +)/ location_continent /(?P \\ d +)/']
Some key things to note, i am using python2.7. 需要注意的一些关键事项,我正在使用python2.7。 Lastly, when i remove the {% url %} from location.html everything works perfectly fine.
最后,当我从location.html删除{%url%}时,一切工作正常。 Here is my working code,
这是我的工作代码,
App/models.py: 应用程序/ models.py:
class Location(models.Model):
title = models.CharField(max_length=255)
location_type = models.CharField(max_length=255, choices=LOCATION_TYPES)
parent = models.ForeignKey("Location", null=True, blank=True,
related_name="parent_location")
def __unicode__(self):
return self.title
class Continent(models.Model):
title = models.CharField(max_length=255)
location = models.OneToOneField(Location, on_delete=models.CASCADE, primary_key=True)
is_an_island = models.BooleanField(default=False)
def __unicode__(self):
return self.location.title
App/views.py: 应用程序/ views.py:
def view_page_location(request, location_id):
location = Location.objects.get(id=location_id)
if location.location_type == 'Continent':
continent = Continent(location=location, is_an_island=False)
return render(request, 'accounts/location.html', {'location':location, 'continent':continent})
def view_continent(request, pk):
get_continent=get_object_or_404(Continent, pk)
return render(request, 'accounts/location_continent.html', {'get_continent':get_continent})
Project/urls.py: 项目/ urls.py:
from App.views import *
url(r'^accounts/location/(?P<location_id>\d+)/', view_page_location, name='location'),
url(r'^accounts/location/(?P<location_id>\d+)/location_continent/(?P<pk>\d+)/', view_continent, name='continent'),
Templates, 模板,
location_tree.html: location_tree.html:
{% for child in locations %}
{% if child.parent == location %}
<ul>
<a href="{% url 'location' location_id=child.id %}">{{ child }}</a>
location.html: location.html:
{% if location.location_type == 'Continent' %}
<h2> Location: <a href="{% url 'continent' pk=location.pk %}">{{ location.title }}</a></h2>
<h3> Type: {{ location.location_type }} </h3></br>
location_continent.html: location_continent.html:
<p> hello </p>
I left location_continent pretty generic because i wanted to see if i can get it to work. 我留下了location_continent相当通用的名称,因为我想看看是否可以使它正常工作。 I feel that something is wrong somewhere in my Urls.py or maybe i'm not properly constructing my views.py.
我觉得Urls.py某处出了点问题,或者可能是我未正确构建views.py。
So the BIG question is, what changes/modifications do i need to alter in order to fix that error? 因此,大问题是,为了修复该错误,我需要进行哪些更改/修改? I can't see it myself so i turn to 'you'.
我自己看不到它,所以我转向“你”。 Any links for me to read up on and find the answer myself is appreciated as well.
任何供我阅读并自己找到答案的链接也将受到赞赏。 I hope my question is clear and not vague.
我希望我的问题是明确的而不是模糊的。
Two issues. 两个问题。
Your continent
url in the location.html
doesn't provide location_id
argument, you provided only pk
. 您在
location.html
中的continent
url不提供location_id
参数,仅提供了pk
。 Change it to something like: 将其更改为:
<a href="{% url 'continent' location_id=location_id pk=location.pk %}">{{ location.title }}</a>
In the urls.py
, you must add $ at the end of the location
url, else there is going to be confusion between location
and continent
urls. 在
urls.py
,必须在location
url的末尾添加$,否则location
和continent
url之间会造成混淆。 $ has a special meaning in regular expression and means that it requires that the pattern matches the end of the string. $在正则表达式中有特殊含义,意味着它要求模式与字符串的结尾匹配。 Change urls to:
将网址更改为:
url(r'^accounts/location/(?P<location_id>\d+)/$', view_page_location, name='location'),
url(r'^accounts/location/(?P<location_id>\d+)/location_continent/(?P<pk>\d+)/', view_continent, name='continent')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.