我不知道为什么我有错误说"反向奖学金'未找到。 ' lscholarship'不是有效的视图函数或模式名称。"当我试图运行http://127.0.0.1:8000/hrfinance/home/时。在我的views.py中,我已经定义了奖学金而不是lscholarship,我在urls.py中写了views.scholarship。
base.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %} Title {% endblock %}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
{% block link %}
{% endblock %}
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
views.py
def scholarship(request, id=None):
query_results = []
if request.POST.get('delete'):
Scholarship.objects.filter(id__in=request.POST.getlist('item')).delete()
return redirect('/hrfinance/lscholarship/')
elif request.POST.get('add'):
form = ScholarshipForm(request.POST)
if form.is_valid():
scholarship = form.save(commit=False)
scholarship.save()
return redirect('/hrfinance/lscholarship/')
else:
form = ScholarshipForm()
id = request.GET.get('scholarship')
query_results = Scholarship.objects.all()
data = {
'query_results':query_results,
'form':form
}
return render(request, 'hrfinance/add_remove_scholarship.html', data)
urls.py
urlpatterns = [
url(r'^home/$', views.home, name='home'),
#timesheet to be filled up by students
url(r'^timesheet/$', views.timesheet, name='timesheet'),
#list of timesheets under 'View Timesheet'
url(r'^ltimesheet/$', views.ltimesheet, name='ltimesheet'),
#list of applications under 'View Application'
url(r'^lapplication/$', views.lapplication, name='lapplication'),
#list of scholarships under 'Add/Remove Scholarship'
url(r'^lscholarship/$', views.scholarship, name='lscholarship'),
url(r'^base/$', views.base, name='base'),
]
下面是追溯:
环境:
请求方法:GET请求网址:http://127.0.0.1:8000/hrfinance/home/
Django版本:1.11.1 Python版本:2.7.13已安装的应用程序: [&#39; hrfinance.apps.HRFinanceConfig&#39;,&#39; django.contrib.admin&#39;, &#39; django.contrib.auth&#39;,&#39; django.contrib.contenttypes&#39;, &#39; django.contrib.sessions&#39;,&#39; django.contrib.messages&#39;, &#39; django.contrib.staticfiles&#39;]已安装的中间件: [&#39; django.middleware.security.SecurityMiddleware&#39 ;, &#39; django.contrib.sessions.middleware.SessionMiddleware&#39 ;, &#39; django.middleware.common.CommonMiddleware&#39 ;, &#39; django.middleware.csrf.CsrfViewMiddleware&#39 ;, &#39; django.contrib.auth.middleware.AuthenticationMiddleware&#39 ;, &#39; django.contrib.messages.middleware.MessageMiddleware&#39 ;, &#39; django.middleware.clickjacking.XFrameOptionsMiddleware&#39;]
模板错误:在模板D:\ curtin \ year3 SEM中 2 \ XiMing_2Jul \ myHDR \ hrfinance \ templates \ hrfinance \ base.html,错误在 第0行反向奖学金&#39;未找到。 &#39; lscholarship&#39;不是 有效的视图函数或模式名称。 1:2: 3:4:5 :{%block title%}标题{%endblock%} 6:
7:{%block link%} 8:{%endblock%} 9:
10:回溯:
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ core \ handlers \ exception.py&#34; 在内心 41. response = get_response(request)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ core \ handlers \ base.py&#34;在 _get_response 187. response = self.process_exception_by_middleware(e,request)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ core \ handlers \ base.py&#34;在 _get_response 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件&#34; D:\ curtin \ year3 SEM 2 \ XiMing_2Jul \ myHDR \ hrfinance \ views.py&#34;在 家 11. return render(request,&#39; hrfinance / home.html&#39;)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ shortcuts.py&#34;在渲染中 30. content = loader.render_to_string(template_name,context,request,using = using)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ loader.py&#34;在 render_to_string 68. return template.render(context,request)
文件 &#34; C:\ Python27 \ lib中\站点包\ django的\模板\后端\ django.py&#34;在 给予 66. return self.template.render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在渲染中 207. return self._render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在 _render 199. return self.nodelist.render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在渲染中 990. bit = node.render_annotated(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在 render_annotated 957. return self.render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ loader_tags.py&#34;在 给予 177. return compiled_parent._render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在 _render 199. return self.nodelist.render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在渲染中 990. bit = node.render_annotated(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在 render_annotated 957. return self.render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ loader_tags.py&#34;在 给予 72. result = block.nodelist.render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在渲染中 990. bit = node.render_annotated(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ base.py&#34;在 render_annotated 957. return self.render(context)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ template \ defaulttags.py&#34;在 给予 458. url = reverse(view_name,args = args,kwargs = kwargs,current_app = current_app)
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ urls \ base.py&#34;相反 91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view,prefix, * args,** kwargs)))
文件&#34; C:\ Python27 \ lib \ site-packages \ django \ urls \ resolvers.py&#34;在 _reverse_with_prefix 497.提出NoReverseMatch(msg)
异常类型:NoReverseMatch at / hrfinance / home / Exception Value: 反向奖学金&#39;未找到。 &#39; lscholarship&#39;不是有效的 查看功能或模式名称。
项目urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hrfinance/', include('hrfinance.urls', namespace='hrfinance')),]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
答案 0 :(得分:0)
在redirect()
,
views
这样的方法
return redirect('hrfinance:lscholarship')
代替,
return redirect('/hrfinance/lscholarship/')
假设您的项目urls.py
包含
url(r'^hrfinance/', include('hrfinance.urls', namespace='hrfinance')),
修改强>
更改您的网址,
url(r'^lscholarship/(?P<id>\d+)/$', views.scholarship, name='lscholarship'),