我无法将我的index.html页与contact.html页连接,当我尝试时它显示了以上错误 使用在Website.urls中定义的URLconf,
Django按以下顺序尝试了这些URL模式:
admin /
[name ='主页']
联系人/
当前路径“ contacts / contact.html”与任何这些都不匹配。
这是我的contacts.urls.py
from django.urls import path
from . import views
urlpatterns = [
path('Contact', views.contacts, name='contact-us')
]
我的contacts.views.py
from django.shortcuts import render
# Create your views here.
def contacts(Request):
return render(Request, 'contact.html')
我的website.urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('Websiteapp.urls')),
path('Contact/', include('contacts.urls'))
]
答案 0 :(得分:1)
模板应具有从任何template
目录开始的完整路径。
由于约定为myappname/templates/myappname/
,因此我们将从templates
之后截取。
像这样
def contacts(Request):
return render(Request, 'myappname/contact.html/')
并且模式应该全部小写,像这样
mywebsite.urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('Websiteapp.urls')),
path('contact/', include('contacts.urls'))
]
contacts.urls
from django.urls import path
from . import views
urlpatterns = [
path('contact', views.contacts, name='contact-us')
]
现在从contact/contact
答案 1 :(得分:1)
您的网址格式建议您应该访问http://127.0.0.1:8000/Contact/Contact
。
答案 2 :(得分:0)
尝试打开http://127.0.0.1:8000/Contact/
当您在urls.py
答案 3 :(得分:0)
Django URL dispatcher doc非常清楚
# In settings/urls/main.py
from django.urls import include, path
urlpatterns = [
path('<username>/blog/', include('foo.urls.blog')),
]
# In foo/urls/blog.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.blog.index),
path('archive/', views.blog.archive),
]