Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/contacts/contact.html

倾然丶 夕夏残阳落幕 提交于 2020-06-29 04:40:10

问题


I fails to connect my index.html page with contact.html page it shows this above error when i tried Using the URLconf defined in Website.urls,

Django tried these URL patterns, in this order:

admin/

[name='home-page']

Contact/

The current path, contacts/contact.html, didn't match any of these.

This my contacts.urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('Contact', views.contacts, name='contact-us')
]

my contacts.views.py

from django.shortcuts import render

# Create your views here.

def contacts(Request):
    return render(Request, 'contact.html')

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

回答1:


Your URL patterns suggest you should access http://127.0.0.1:8000/Contact/Contact.




回答2:


The template should be with the full path starting from any of the template directories.

since the convention is myappname/templates/myappname/, we'll clip from after templates.

like this

def contacts(Request):
    return render(Request, 'myappname/contact.html/')

and the pattern should be all lower case, like this 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')
]

now access it from contact/contact




回答3:


Try open http://127.0.0.1:8000/Contact/ as you define /Contact/ in your urls.py




回答4:


Django URL dispatcher doc is very clear

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


来源:https://stackoverflow.com/questions/60938227/page-not-found-404-request-methodget-request-urlhttp-127-0-0-18000-cont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!