【问题标题】:How to change the url of an app in Django to custom url如何将 Django 中的应用程序的 url 更改为自定义 url
【发布时间】:2021-07-29 07:20:25
【问题描述】:

我的 Django 项目中有一个名为 Stressz 的应用程序。所以我的应用程序的网址是:

http://localhost:8000/stressz/
http://localhost:8000/stressz/siker
http://localhost:8000/stressz/attitud

如何在不将应用名称从上述 url 更改为如下内容的情况下更改 url:

http://localhost:8000/mpa
http://localhost:8000/mpa/siker
http://localhost:8000/mpa/attitud

urls.py

app_name = 'stressz'
urlpatterns = [
    path('', views.index, name='index'),
    path('siker', views.siker, name='siker'),
    path('attitud', login_required(views.AttitudCreateView.as_view()), name='attitud_item'),
    ...

【问题讨论】:

    标签: python django url


    【解决方案1】:

    查看您的根 urls.py,您通常可以在您的项目(而不是应用程序)文件夹中找到它。必须有类似这样的一行:

    urlpatterns = [
        path('stressz', include('stressz.urls')),
    ]
    

    如果您将其更改为:

    urlpatterns = [
        path('mpa', include('stressz.urls')),
    ]
    

    它应该按您的预期工作。

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2021-05-20
      • 2019-11-24
      • 2015-06-19
      相关资源
      最近更新 更多