开发者

Django URLS, how to map root to app?

开发者 https://www.devze.com 2023-04-09 03:36 出处:网络
I am pretty new to django but experienced in Python and java web programming with different frameworks.

I am pretty new to django but experienced in Python and java web programming with different frameworks. I have made myself a nice little django app, but I cant seem to make it match www.mysite.co开发者_开发技巧m as opposed to www.mysite.com/myapp.

I have defined urls and views in my urls.conf which is currently not decoupled from the app (don't mind that).

urlpatterns = patterns('myapp.views',
  (r'^myapp/$', 'index'),
  (r'^myapp/(?P<some_id>\d+)/global_stats/$', 'global_stats'),
  (r'^myapp/(?P<some_id>\d+)/player/(?P<player_id>\d+)/$', 'player_stats'),
)

All this works like a charm. If someone goes to www.mysite.com/myapp they will hit my index view which causes a http redirect to my "correct" default url.

So, how can I add a pattern that will do the same as (r'^myapp/$', 'index') but without the /myapp--that is, www.mysite.com should suffice?

I would think this would be very basic stuff... I tried adding a line like:

(r'^$', 'index'),

however this throws me in a loop...

Hope you django gurus out there can clarify this for me!


I know that this question was asked 2 years ago, but I've faced the same problem and found a solution:

In the project urls.py:

urlpatterns = patterns('',
    url(r'^', include('my_app.urls')), #NOTE: without $
)

In my_app.urls.py:

urlpatterns = patterns('',
    url(r'^$', 'my_app.views.home', name='home'),
    url(r'^v1/$', 'my_app.views.v1', name='name_1'),
    url(r'^v2/$', 'my_app.views.v2', name='name_2'),
    url(r'^v3/$', 'my_app.views.v3', name='name_3'),
)


This sounds strange.

Your latest attempt should work, but what I normally do - put

urlpatterns = patterns('',
    (r'^$', lambda r: HttpResponseRedirect('myapp/')),
    ...
)

This scales better when you start adding new apps.


As I didn't see any answer for django 2.0 I thought I'll provide one. you have to use '' as the root url. Here is an example from django 2.0 docs

urlpatterns = [
    path('', main_views.homepage),
    path('help/', include('apps.help.urls')),
    path('credit/', include(extra_patterns)),
]


Just put an empty raw regular expression: r''

I tested here and it worked perfectly.

urlpatterns = patterns('',
    url(r'', include('homepage.urls')),
    url(r'^homepage/', include('homepage.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

Hope it help!


I also wanted to have the root of my domain to directly point to a view of a sub app.

At first I created the sub app with this command:

python3 manage.py startapp main offer_finder/main

In your case it would be:

python3 manage.py startapp myApp project/somedirectory/myApp

make sure that the directory exists: mkdir -p project/somedirectory/myApp

This is my project structure: Project structure

In my case I have these directories:

offer_finder_project/offer_finder/main        # sub app  
offer_finder_project/offer_finder/            # main source directory  

in offer_finder_project/offer_finder/urls.py I have this code:

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('offer_finder.main.urls')),
]

And in offer_finder_project/offer_finder/main/urls.py I have this code:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),
]

And the offer_finder_project/offer_finder/main/views.py file simply contains some test code.

from django.http import HttpResponse

def index(request):
    return HttpResponse("TEST app index")

This way your requests to your root domain are directed to your sub app. Hopefully this helps someone. This code was tested with Django 2.1.4.


old question here ... (as much as myself :o)
but still worth of sharing updates for Django 3.x:

In project urls.py just add:

urlpatterns = [
    path('', include('myapp.urls')), 
    path('myapp/', include('myapp.urls')),
    #path('admin/', admin.site.urls),
]

Be aware that this solution will monopolize the whole django to your hungry&selfish app :-) !!!


I know the answer is late, but i had my fair share of hunting recently. This is what i tried with CBV.. in Project urls.py

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

PS: It is always recommended to use namespace. Gives a good advantage later on.

In App urls.py

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号