EazyC
EazyC

Reputation: 819

Django URL Mapping: How to remove app name from URL paths?

I have a Django app named 'myapp' and my index page in my url.py in the "myapp" directory as:

 url(r'^$', views.index, name='index')

So this essentially makes the URL in the test server http://127.0.0.1:8000/myapp/

However, I don't want this. I want every url to start simply at http://127.0.0.1:8000/ so a page that is currently only accessible at http://127.0.0.1:8000/myapp/bigduck on the test server, I want to instead make it accessible only at http://127.0.0.1:8000/bigduck/

Basically, I want to just remove the app name from the url mapping scheme of this app.

How can I do this? Thanks in advance.

Upvotes: 3

Views: 6890

Answers (2)

philmaweb
philmaweb

Reputation: 533

Update 2019 / django >= 2.0

Edit your main urls.py - the one in the same directory as your settings.py file.

urlpatterns = [
    # updated so we don't need the myapp prefix
    # path('myapp/', include('myapp.urls')), # old version
    path('', include('myapp.urls')),
    path('admin/', admin.site.urls),
]

Upvotes: 4

bakkal
bakkal

Reputation: 55448

You should look inside the main urls.py file of your project (e.g. the one settings.ROOT_URLCONF points to, not the one inside the individual apps)

In that main urls.py you will see an entry that maps the urls of your myapp to the URL path myapp/.

You can replace that with something like this:

url(r'^$', 'myapp.urls')

Which will put all the urls under myapp, on the top level path, so you won't need to use myapp/

Edit

You can also map bigduck in your main urls.py, e.g.

bigduck/ -> myapp.bigduck
register/ -> register

but what I meant in my comment is that the order in which the regex of URLs appear in your urls.py file is important, because Django will stop at the first match, that's why all your URLs are mapped to the myapp

Look at number 3 here https://docs.djangoproject.com/en/dev/topics/http/urls/#how-django-processes-a-request

Upvotes: 5

Related Questions