2017-11-16 9 views
0

Hello StackOverFlowメンバーまで、私の考え方やプロセスをここに戻す前に、私の問題をさらに細くするのを助けてください。 「location_tree.html」内のロケーションオブジェクトをクリックすると、ロケーション名とそのタイプを表示する新しいページ「location.html」にリダイレクトされます。まったく同じページから、その名前は「場所」に関するより詳細な別のページへのハイパーリンクになります。上記Django Reverse Error:NoReverseMatch at

は私がしたい一般的な流れであるが、私はlocation.htmlから名前をクリックしようとすると、それはこのエラーに私をリダイレクトします。

NoReverseMatch at /accounts/location/2/ Reverse for 'continent' with keyword arguments '{u'pk': 2}' not found. 1 >pattern(s) tried: ['accounts/location/(?>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:私はそれを得ることができるかどうかを確認したかったので、私はかなり一般的なlocation_continentを左

<p> hello </p> 

作業。私は何かが私のUrls.pyのどこかで間違っていると感じ、あるいはおそらく私は適切に私のviews.pyを構築していません。

BIGの質問には、そのエラーを修正するために変更する必要がある変更や修正がありますか?私は自分自身を見ることができないので、私はあなたに向ける。私が読んで答えを見つけ出すためのリンクもありがたいです。私は私の質問がはっきりしていて曖昧でないことを望む。

答えて

1

2つの問題。

location.htmlcontinentのURLにはlocation_id引数がありません。pkと入力してください。以下のようなもののように変更します。urls.py

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

は、あなたが他のlocationcontinent URL間の混乱があるように起こっている、location URLの末尾に$を追加する必要があります。 $は正規表現で特別な意味を持ち、パターンが文字列の終わりと一致することを要求します。 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') 
+0

ありがとうございました!それはトリックをしたURLの位置引数でした! – ecosimon