1

我正在尝试在模板文件中创建当前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.

网址.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'),
)

视图.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()

圈数.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 %}
4

3 回答 3

6

解决了以下问题:

<td><a href="{% url 'laptimes:lap' lap.id %}">Edit</a></td>
于 2013-09-21T10:01:42.907 回答
2

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

<td>{% url 'lap' lap.id %}</td>
于 2013-09-21T09:30:06.730 回答
1

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

这些中的任何一个都应该起作用:

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

现在不松懈了!

于 2013-09-21T10:25:45.423 回答