【问题标题】:Django {% url %} NoReverseMatchDjango {% url %} NoReverseMatch
【发布时间】:2013-09-26 16:20:11
【问题描述】:

我正在尝试在模板文件中创建当前Laps 对象ID 的链接,我终生无法解决此错误。

这是我的:

错误

Request Method: GET
Request URL:    http://localhost:8000/laps/
Django Version: 1.5.2
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'views.LapView' with arguments '(181,)' and keyword arguments '{}' not found.

urls.py

from django.conf.urls import patterns, include, url
from django.core.urlresolvers import reverse_lazy
from django.views.generic import RedirectView
from laptimes import views

urlpatterns = patterns('',
    url(r'^$', RedirectView.as_view(url=reverse_lazy('laptimes:laps')), name='index'),
    url(r'^laps/$', views.LapsView.as_view(), name='laps'),
    url(r'^laps/powerlaps/$', views.PowerLapsView.as_view(), name='powerlaps'),
    url(r'^laps/starcar/$', views.StarCarView.as_view(), name='starcar'),
    url(r'^laps/formulaone/$', views.FormulaOneView.as_view(), name='formulaone'),
    # cars
    url(r'^$', RedirectView.as_view(url=reverse_lazy('laptimes:cars')), name='index'),
    url(r'^cars/$', views.CarsView.as_view(), name='cars'),
    url(r'^cars/manu/$', views.CarsManuView.as_view(), name='carsmanu'),
    # drivers
    url(r'^$', RedirectView.as_view(url=reverse_lazy('laptimes:drivers')), name='index'),
    url(r'^drivers/$', views.DriversView.as_view(), name='drivers'),
    url(r'^drivers/nat/$', views.DriversNatView.as_view(), name='driversnat'),
    # episodes
    url(r'^$', RedirectView.as_view(url=reverse_lazy('laptimes:episodes')), name='index'),
    url(r'^episodes/$', views.EpisodesView.as_view(), name='episodes'),
    # single lap
    url(r'^lap/(?P<pk>\d+)/$', views.LapView.as_view(), name='lap'),
)

views.py

# Create your views here.
from django.views import generic
from laptimes.models import *


class LapView(generic.DetailView):
    model = Lap
    template_name = 'laptimes/lap.html'



class LapsView(generic.ListView):

    template_name = 'laptimes/laps.html'
    context_object_name = 'laps'

    def get_queryset(self):
        return Lap.objects.all()

laps.html

{% extends 'laptimes/base.html' %}
{% block content %}
<h1>All Laps</h1>
{% if laps %}
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Ref</th>
                <th>Time</th>
                <th>Car</th>
                <th>Driver</th>
                <th>Episode</th>
                <th>Conditions</th>
                <th>Comment</th>
            </tr>
        </thead>
        <tbody>
        {% for lap in laps %}
            <tr>
                <td>{% url 'views.LapView' lap.id %}</td>
                <td>{{ lap.time_str }}</td>
                <td>{{ lap.car }}</td>
                <td>{{ lap.driver }}</td>
                <td>
                    {{ lap.episode|default_if_none:"" }}
                    {{ lap.episode.date|default_if_none:"" }}
                </td>
                <td>{{ lap.condition|default_if_none:"" }}</td>
                <td>{{ lap.comment|default_if_none:"" }}</td>
            </tr>
        {% endfor %}
        </tbody>
        </table>
{% else %}
    <p>No laps are available.</p>
{% endif %}
{% endblock %}

【问题讨论】:

    标签: python django url views


    【解决方案1】:

    解决了以下问题:

    <td><a href="{% url 'laptimes:lap' lap.id %}">Edit</a></td>
    

    【讨论】:

      【解决方案2】:

      不使用视图函数,而是使用url名称作为

      <td>{% url 'lap' lap.id %}</td>
      

      【讨论】:

      • Reverse for 'lap' with arguments '(181,)' and keyword arguments '{}' not found.:S
      • @ツ.jp,或试试{% url 'lap' pk=lap.id %}
      【解决方案3】:

      您需要提供明确的完全限定名称。使用视图的完整 Python 路径或完全命名空间的视图名称。

      这些都应该工作:

      {% url 'laptimes.views.LapView' lap.id %}
      {% url 'laptimes:lap' lap.id %}
      

      现在不能松懈!

      【讨论】:

      • 但是为什么呢?文档说我应该输入 the url name 并完成它。
      猜你喜欢
      • 2013-01-19
      • 2015-07-18
      • 1970-01-01
      • 2017-03-14
      • 2016-06-04
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2018-12-17
      相关资源
      最近更新 更多