Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

python - Django, Path return 404

I've got an issue with my Django app, when i try to browse to http://127.0.0.1:8000/list/ it returns a 404 error :

Using the URLconf defined in f_django.urls, Django tried these URL patterns, in this order:

^contact/$
^description/$
^$
myapp/
admin/
The current path, list/, didn't match any of these.

Here my urls.py file :

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from django.urls import include    
from . import views

urlpatterns = [
    url(r'^contact/$',views.contact),
    url(r'^description/$',views.description),
    url(r'^$',include('myapp.urls')),
    path('myapp/',include('myapp.urls')),
    path('admin/', admin.site.urls),
]

My myapp/urls.py file :

from django.conf.urls import url
from django.urls import path
from . import views

urlpatterns = [
    url(r'^$',views.index),
    url(r'^list',views.list),
    url(r'^Articles/(?P<id>[0-9]+)$', views.details),
]

And this line in my settings.py :

INSTALLED_APPS = [
    'myapp.apps.MyappConfig',
]

Thanks in advance for your help

question from:https://stackoverflow.com/questions/66049340/django-path-return-404

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change '^list' to '^list/$', having your urls end with a / is important because Django appends slashes by default to any incoming url which doesn't end in one. You can change this by setting APPEND_SLASH = False in the settings. Also you are including myapp.urls twice in your project level urls.
Also in your include:

url(r'^$',include('myapp.urls'))

You are including urls but in the pattern you write ^$. In regex ^ means the string should start from that position and $ means the string should end at that position. Your pattern for views.list end up being ^$^list/$ which is impossible to match. Basically you are preventing any included urls from being matched if you do this. Remove the $ from there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...