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.com as opposed to www.mysite.com/myapp.
我是django的新手,但在使用不同框架的Python和java web编程方面经验丰富。我已经为自己做了一个不错的小django应用程序,但我似乎无法使它与www.mysite.com相匹配而不是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).
我在urls.conf中定义了网址和视图,这些网址目前没有与应用程序分离(不介意)。
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.
所有这一切都像一个魅力。如果有人访问www.mysite.com/myapp,他们将点击我的索引视图,这会导致http重定向到我的“正确”默认网址。
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?
那么,我如何添加一个与(r'^ myapp / $','index')相同的模式但没有/ myapp - 也就是说,www.mysite.com就足够了?
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!
希望你在那里的django大师可以为我澄清这个!
5 个解决方案
#1
57
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.
当您开始添加新应用时,这会更好地扩展。
#2
93
I know that this question was asked 2 years ago, but I've faced the same problem and found a solution:
我知道这个问题是2年前提出的,但我遇到了同样的问题并找到了解决方案:
In the project urls.py
:
在项目urls.py中:
urlpatterns = patterns('',
url(r'^', include('my_app.urls')), #NOTE: without $
)
In my_app.urls.py
:
在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'),
)
#3
8
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
由于我没有看到django 2.0的任何答案,我以为我会提供一个。你必须使用''作为根网址。这是django 2.0 docs的一个例子
urlpatterns = [
path('', main_views.homepage),
path('help/', include('apps.help.urls')),
path('credit/', include(extra_patterns)),
]
#4
4
Just put an empty raw regular expression: r''
只需放一个空的原始正则表达式: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!
希望它有所帮助!
#5
1
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
我知道答案已经很晚了,但最近我有相当多的狩猎。这是我在项目urls.py中尝试使用CBV ..
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.
PS:始终建议使用命名空间。以后会给你一个很好的优势。
In App urls.py
在App urls.py中
urlpatterns = [
url(r'^$', views.IndexPageView.as_view(), name='index'),
]
#1
57
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.
当您开始添加新应用时,这会更好地扩展。
#2
93
I know that this question was asked 2 years ago, but I've faced the same problem and found a solution:
我知道这个问题是2年前提出的,但我遇到了同样的问题并找到了解决方案:
In the project urls.py
:
在项目urls.py中:
urlpatterns = patterns('',
url(r'^', include('my_app.urls')), #NOTE: without $
)
In my_app.urls.py
:
在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'),
)
#3
8
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
由于我没有看到django 2.0的任何答案,我以为我会提供一个。你必须使用''作为根网址。这是django 2.0 docs的一个例子
urlpatterns = [
path('', main_views.homepage),
path('help/', include('apps.help.urls')),
path('credit/', include(extra_patterns)),
]
#4
4
Just put an empty raw regular expression: r''
只需放一个空的原始正则表达式: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!
希望它有所帮助!
#5
1
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
我知道答案已经很晚了,但最近我有相当多的狩猎。这是我在项目urls.py中尝试使用CBV ..
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.
PS:始终建议使用命名空间。以后会给你一个很好的优势。
In App urls.py
在App urls.py中
urlpatterns = [
url(r'^$', views.IndexPageView.as_view(), name='index'),
]