简体   繁体   中英

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. 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. 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:

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+)/']

Some key things to note, i am using python2.7. Lastly, when i remove the {% url %} from location.html everything works perfectly fine. Here is my working code,

App/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:

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:

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:

{% 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>

I left location_continent pretty generic because i wanted to see if i can get it to work. I feel that something is wrong somewhere in my Urls.py or maybe i'm not properly constructing my 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 . 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. $ 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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