Django与{%url%}中的视图没有反向匹配

时间:2018-04-10 16:17:28

标签: django

使用Django 2.0.3时,我收到错误

找不到'detail_view'的反转。 'detail_view'不是有效的视图函数或模式名称。

在我的模板中突出显示此行(results / list.html):

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

(如果重要的话,这行是{%for results in results%}循环;我没有包含html文件的其余部分,因为正在显示标签)

主要的urls.py文件是

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()

并且结果应用程序的urls.py文件是

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'),
]

结果应用的views.py文件是

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

每个有类似问题的人在他们的urls.py或html文件中都有一些拼写错误,但据我所知,我没有常见的拼写错误。我是瞎了还是别的错了?

2 个答案:

答案 0 :(得分:2)

您还必须在链接中插入应用名称,如:

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

这必须正常工作

答案 1 :(得分:0)

您不需要包含intron_id。 Django将按顺序放置任何变量

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