Devlin Moyer
Devlin Moyer

Reputation: 35

Django no reverse match for view in {% url %}

When using Django 2.0.3, I get the error

Reverse for 'detail_view' not found. 'detail_view' is not a valid view function or pattern name.

With this row highlighted in my template (results/list.html):

data-href="{% url 'detail_view' intron_id=hit.0 %}"

(If it matters, this line is in a {% for hit in results %} loop; I didn't include the rest of the html file because the tags were being displayed)

The main urls.py file is

from django.contrib import admin
from django.urls import path, include
from sitepages import views
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name="home"),
    path('about/', views.about, name="about"),
    path('results/', include('results.urls')),
]

urlpatterns += staticfiles_urlpatterns()

and the results app's urls.py file is

from django.contrib import admin
from django.urls import path, re_path
from . import views

app_name='results'

urlpatterns = [
    path('list', views.list),
    re_path('(?P<intron_id>\w+)/$', views.detail_view, name='detail_view'),
]

The views.py file for the results app is

from django.shortcuts import render
import apsw
import re
from .models import Introns

#parse input from search bar and get requested info from database
def detail_view(request, id):
    conn = apsw.Connection('subset.db')
    cur = conn.cursor()
    #just get one example intron so that we can format the individual intron view page
    cur.execute("SELECT * FROM introns WHERE id=?", id)
    data = cur.fetchall()
    return render(request, 'results/individual.html', {'data':data})

def list(request):
    #another view that returns results/list.html without errors if I try to navigate directly to it without giving it any data to display

Everyone with similar problems has had some typo in their urls.py or html file, but as far as I can tell, I don't have the common typos. Am I blind, or is something else wrong?

Upvotes: 2

Views: 346

Answers (2)

HenryM
HenryM

Reputation: 5793

You do not need to include intron_id. Django will place any variables in order

data-href="{% url 'detail_view' hit.0 %}"

Upvotes: 0

dstrants
dstrants

Reputation: 7705

You also have to insert the app name in your link like:

data-href="{% url 'results:detail_view' intron_id=hit.0 %}"

this must work just fine

Upvotes: 3

Related Questions