Django URLS,如何将根映射到应用程序? [英] Django URLS, how to map root to app?

查看:56
本文介绍了Django URLS,如何将根映射到应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对django相当陌生,但是在不同框架的Python和Java Web编程方面经验丰富。
我已经使自己成为一个不错的django小应用程序,但是我似乎无法使其与www.mysite.com相匹配,而不是与www.mysite.com/myapp相匹配。

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.

我在urls.conf中定义了url和视图,而该URL和视图目前尚未与应用程序分离(不要介意)。

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'),
)

所有这一切都像一个咒语。如果有人访问www.mysite.com/myapp,他们将进入我的索引视图,这将导致http重定向到我的正确默认网址。

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.

所以,我该怎么办添加一个与(r'^ myapp / $','index')相同但不带/ myapp的模式-也就是说,www.mysite.com应该足够吗?

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...

希望您的django专家可以为我澄清一下!

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

推荐答案

您最近的尝试应该可以,但是我通常会做-把

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.

这篇关于Django URLS,如何将根映射到应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆