ツ.jp
ツ.jp

Reputation: 315

Django {% url %} NoReverseMatch

I'm trying to make a link of the current Laps object ID in a template file, and for the life of me I cannot solve this error.

Here's my:

error

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 %}

Upvotes: 1

Views: 5189

Answers (3)

ツ.jp
ツ.jp

Reputation: 315

Solved the problem with the following:

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

Upvotes: 6

Filip Dupanović
Filip Dupanović

Reputation: 33670

You need to provide unambiguous fully qualified names. Either use the full Python path to your view or the fully namespaced view name.

Either of these should work:

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

No slacking now!

Upvotes: 1

Rohan
Rohan

Reputation: 53366

Instead of using the view function, use url name as

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

Upvotes: 2

Related Questions