没有ReverseMatch URL django

时间:2016-02-29 22:14:33

标签: django templates hyperlink views reverse

我有一个看起来像这样的场景:

模型

from django.db import models

from django.core.urlresolvers import reverse_lazy, reverse

class State(models.Model):
    short_name = models.CharField('Abbreviation', max_length=2)
    state = models.SlugField('State Name', max_length=20)

    def __str__(self):
        return self.state

    def get_absolute_url(self):
        return reverse('state_list', kwargs={'state': self.state})

class City(models.Model):
    state = models.ForeignKey(State)
    city = models.CharField('City', max_length=100)

    class Meta:
        verbose_name = 'City'
        verbose_name_plural = 'Cities'

    def __str__(self):
        return self.city

class School(models.Model):
    name = models.CharField(max_length=69, default='')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'School'
        verbose_name_plural = 'Schools'

浏览

from django.shortcuts import get_object_or_404, render
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView

from django.core.urlresolvers import reverse_lazy

from .models import School, City, State

def reviews_index(request):
    state_list = State.objects.all()
    context = {'states': state_list}
    return render(request, 'reviews/reviews_index.html', context)

def state_detail(request, state=None):
    city_list = City.objects.order_by('city')
    context = {'cities': city_list}
    return render(request, 'reviews/state_detail.html', context)

def city_detail(request, state=None, city=None):
    school_list = School.objects.all()
    context = {'schools': school_list}
    return render(request, 'reviews/city_detail.html', context)

网址

from django.conf.urls import url, include

from . import views 

app_name = 'reviews'
urlpatterns = [
    url(r'^$', views.reviews_index, name='reviews_index'),
    url(r'^(?P<state>[a-z]+)/$', views.state_detail, name='state_detail'),
    url(r'^(?P<state>[a-z]+)/(?P<city>[a-z]+)/$', views.city_detail, name='city_detail'),

]

但是,当我尝试创建一个从state_detail模板到city_detail模板的链接时,我收到此错误:

NoReverseMatch at /reviews/alabama/
Reverse for 'city_detail' with arguments '('', 'Auburn')' and keyword arguments '{}' not found. 0 pattern(s) tried: []

这是我在模板中链接的方式:

{% block main_content %}
    <div class="content">
        <div class="row">
            {% if cities %}
            {% for city in cities %}
            <div class="medium-3 column">
                <a href="{% url 'city_detail' state.state city.city %}">{{ city.city }}</a>
            </div>
            {% endfor %}
            {% endif %}
        </div>
    </div>
{% endblock %}

有人可以告诉我我做错了并帮我解决了。提前谢谢。

1 个答案:

答案 0 :(得分:1)

city_detail网址格式包含state.state,因此您需要在模板上下文中包含该状态。

在您看来,您可以使用get_object_or_404使用slug获取状态。

def state_detail(request, state=None):
    state = get_object_or_404(State, state=state)
    city_list = City.objects.filter(state=state).order_by('city')
    context = {'cities': `city_list`, 'state': state}
    return render(request, 'reviews/state_detail.html', context)

请注意,我已更改city_list,因此它仅显示您正在查看的州内的城市。

对于实例和slug使用相同的变量名state并不是一个好主意。重命名其中一个是个好主意,例如到state_objstate_slug。如果您这样做,则必须确保更新您的网址,视图和模板以保持一致。