如何将我的Django路径设置为http://127.0.0.1:8000/myproject/admin

时间:2019-01-09 14:28:35

标签: python django

这是我的myproject / urls.py

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

当我运行python manage.py runserver 127.0.0.1:8000时 工作正常 http://127.0.0.1:8000 http://127.0.0.1:8000/admin http://127.0.0.1:8000/myapp

但是我想要这样

http://127.0.0.1:8000/myproject http://127.0.0.1:8000/myproject/admin http://127.0.0.1:8000/myproject/myapp

,所有URL甚至模板URL都应以相同的模式进行重定向。

在这里我可以在path / Setting中设置单个更改,以使用/ myproject /重定向以正常工作。然后我可以在一个地方将/ myproject /更改为/ project2/。

实际上,我是从nginx运行项目的,

server {
  location / {
                  proxy_pass http://127.0.0.1:8000/;
                  proxy_http_version 1.1;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection 'upgrade';
                  proxy_set_header Host $host;
                  proxy_cache_bypass $http_upgrade;
          }
is works fine for http://127.0.0.8000/

but where i can set the name in one place like
  location /myname/ {
 ...
}
so that my project will comes under http://127.0.0.8000/myname/myprojet

2 个答案:

答案 0 :(得分:2)

考虑到将来的更新,最好的方法可能是相对于/myproject路径创建一个新的顶级urls文件,该文件又会导入您当前的urls文件。像这样:

urlpatterns = [
   url(r'^myproject/', include('myproject.urls')
]

但是由于您只想在所有URL的开头添加 myproject ,而URL匹配使用正则表达式,因此您可以这样做:

urlpatterns = [
    url(r'^myproject/$', index, name='index'),
    url(r'^myproject/admin/', admin.site.urls),
    url(r'^myprojet/myapp/', include('myapp.urls'))
]

答案 1 :(得分:0)

另一种方法是将root映射到您的项目,并将root映射到您的应用。

赞:

在项目urls.py中:

urlpatterns = patterns('',
    url(r'^myproject', include('myproject.urls')), 
)

如果您打算将新应用添加到项目中,则可以进行编辑 在myapp.urls.py中:

urlpatterns = patterns('',
    url(r'^$', 'myapp.views.home', name='home'),     # http://127.0.0.1:8000/myproject/myapp
    url(r'^v1/$', 'myapp.views.v1', name='name_1'),  # http://127.0.0.1:8000/myproject/myapp/name_1
    url(r'^v2/$', 'myapp.views.v2', name='name_2'),  # http://127.0.0.1:8000/myproject/myapp/name_2
    url(r'^v3/$', 'myapp.views.v3', name='name_3'),  # http://127.0.0.1:8000/myproject/myapp/name_3
)

取决于您的django文档,请参见语法:django docs older 2.0django 2.1 docs

希望有帮助!