how to set my django path to http://127.0.0.1:8000/myproject/admin

Viewed 2484

here is my myproject/urls.py

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

when i run python manage.py runserver 127.0.0.1:8000 its working fine http://127.0.0.1:8000 http://127.0.0.1:8000/admin http://127.0.0.1:8000/myapp

but I want to like this

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

and all URLs even templates URLs should redirect in the same pattern.

where i can set the single changes in path/Setting to redirect with /myproject/ to work fine. and later I can change with /myproject/ to /project2/ in a single place.

actually, I am running my project from 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 Answers

The best way to do this with future updates in mind would probably be by creating a new top-level urls file relative to the /myproject path which in turn imports your current urls file. Like so:

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

But since you just want to add myproject to the beginning of all urls without discretion and the url matching uses regex, you could just do:

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

another way is to map root to your project and root to your apps.

Like this:

In the project urls.py:

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

if you intent to adding new apps to your projects, you may edit In 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
)

depending on your django docs , see syntax : django docs older 2.0 or django 2.1 docs

Hope it help!

Related