简体   繁体   English

Django URL映射有问题

[英]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. 我正在为即将到来的项目尝试Django,并通过了一个教程并遇到定义URL路径的问题。 Below is the project structure showing the urls.py at the project root. 下面是在项目根目录下显示urls.py的项目结构。 The urls.py in my timer package is pretty simple: 我的计时器包中的urls.py非常简单:

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? 但是,当我启动该应用程序并导航到localhost:8080 / timer /时,我得到的是404。我应该看什么建议?

项目结构

You can try to change URLs in the settings of the whole project like this: 您可以尝试在整个项目的设置中更改URL,如下所示:

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

As for the timer, the URLs will be like this: 至于计时器,URL将如下所示:

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

Don't forget about r's in the url(). 不要忘记url()中的r's

It turns out that I created the urls.py under a wrong folder. 原来,我在错误的文件夹下创建了urls.py。 I misunderstood the instructions and created the urls.py in the same folder as manage.py . 我误解了说明,并在与manage.py相同的文件夹中创建了urls.py。 Once I add the new url pattern in the file projectdb/projectgb/urls.py, the issue is fixed. 一旦我在文件projectdb / projectgb / urls.py中添加了新的网址格式此问题就得到解决。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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