繁体   English   中英

Django反向错误:NoReverseMatch位于

[英]Django Reverse Error: NoReverseMatch at

您好StackOverFlow成员们,在我深入探讨之前,让我在这里追溯我的想法/过程,以帮助进一步解决我的问题。 当我单击“ location_tree.html”中的位置对象时,它将重定向到新页面“ location.html”,显示位置名称及其类型。 在同一页面上,该名称将是到另一个页面的超链接,其中包含有关“位置”的更多详细信息。

上面是我想要的一般流程,但是当我尝试单击location.html中的名称时,它会将我重定向到此错误:

找不到/ accounts / location / 2 /上的NoReverseMatch,反向搜索带有关键字参数'{u'pk':2}'的'continent'。 1>尝试过的模式:['帐户/位置/(?> P \\ d +)/ location_continent /(?P \\ d +)/']

需要注意的一些关键事项,我正在使用python2.7。 最后,当我从location.html删除{%url%}时,一切工作正常。 这是我的工作代码,

应用程序/ 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

应用程序/ 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})

项目/ 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'),

模板,

location_tree.html:

{% for child in locations %}
                {% if child.parent == location %}
                    <ul>
                        <a href="{% url 'location' location_id=child.id %}">{{ child }}</a>

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:

<p> hello </p>

我留下了location_continent相当通用的名称,因为我想看看是否可以使它正常工作。 我觉得Urls.py某处出了点问题,或者可能是我未正确构建views.py。

因此,大问题是,为了修复该错误,我需要进行哪些更改/修改? 我自己看不到它,所以我转向“你”。 任何供我阅读并自己找到答案的链接也将受到赞赏。 我希望我的问题是明确的而不是模糊的。

两个问题。

您在location.html中的continent url不提供location_id参数,仅提供了pk 将其更改为:

<a href="{% url 'continent' location_id=location_id pk=location.pk %}">{{ location.title }}</a>

urls.py ,必须在location url的末尾添加$,否则locationcontinent url之间会造成混淆。 $在正则表达式中有特殊含义,意味着它要求模式与字符串的结尾匹配。 将网址更改为:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM