python-无法获取django声明的应用程序路由作为页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-无法获取django声明的应用程序路由作为页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2389字,纯文字阅读大概需要4分钟。
内容图文
我正在创建Django应用,并且遇到路由未识别的问题.这是我尝试创建的同一DJango民意测验应用程序,但文档代码不起作用.这是我的代码如下:
djangoproject / urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^simpleapp/', include('simpleapp.urls')),
url(r'^admin/', admin.site.urls),
]
simpleapp / views.py
from django.shortcuts import render
from django.http import HttpResponse, request
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
simpleapp / urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /simpleapp/
# url('', views.index, name='index'),
# ex: /simpleapp/5/
url('<int:question_id>/', views.detail, name='detail'),
# ex: /simpleapp/5/results/
url('<int:question_id>/results/', views.results, name='results'),
# ex: /simpleapp/5/vote/
url('<int:question_id>/vote/', views.vote, name='vote'),
]
如果我取消注释simpleapp / urls.py代码的”路径的第一个URL,则显示的所有页面均为”路径.但是,如果我将url”路径保留为注释,则路由会给我以下错误:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/simpleapp/34/
Using the URLconf defined in simple_django.urls, Django tried these URL patterns, in this order:
^simpleapp/ <int:question_id>/ [name='detail']
^simpleapp/ <int:question_id>/results/ [name='results']
^simpleapp/ <int:question_id>/vote/ [name='vote']
^admin/
The current path, simpleapp/34/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
我无法使用django.conf.urls或django.urls导入path(). url()成功.我正在使用python版本3.6.7和Django版本2.1.5.我想念什么?
解决方法:
您在simpleapp / urls.py中使用的是路径语法,而不是正则表达式.更改为:
urlpatterns = [
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
您的项目URL使用的是正则表达式,因此尽管您可能希望使它们保持一致,但不应更改它们.
内容总结
以上是互联网集市为您收集整理的python-无法获取django声明的应用程序路由作为页面全部内容,希望文章能够帮你解决python-无法获取django声明的应用程序路由作为页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。