我正在尝试在模板文件中创建当前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 %}
答案 0 :(得分:6)
解决了以下问题:
<td><a href="{% url 'laptimes:lap' lap.id %}">Edit</a></td>
答案 1 :(得分:2)
不使用视图功能,而是使用网址名称
<td>{% url 'lap' lap.id %}</td>
答案 2 :(得分:1)
您需要提供明确的完全限定名称。使用视图的完整Python路径或完全命名空间的视图名称。
其中任何一个都应该有效:
{% url 'laptimes.views.LapView' lap.id %}
{% url 'laptimes:lap' lap.id %}
现在没有松懈!