简体   繁体   中英

Having issues with Django URL mapping

I am trying Django for an upcoming project and going through a tutorial and running into issue with defining URL paths. Below is the project structure showing the urls.py at the project root. The urls.py in my timer package is pretty simple:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

However when I launch the application and navigate to localhost:8080/timer/ I am getting a 404. Any suggestions what I should be looking at?

项目结构

You can try to change URLs in the settings of the whole project like this:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^timer/$', include('timer.urls')),
]

As for the timer, the URLs will be like this:

urlpatterns = [
    url(r'^$', views.index, name="index"),
]

Don't forget about r's in the url().

It turns out that I created the urls.py under a wrong folder. I misunderstood the instructions and created the urls.py in the same folder as manage.py . Once I add the new url pattern in the file projectdb/projectgb/urls.py, the issue is fixed. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM