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