簡體   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