问题描述
我有一个正在生产的django项目,目前仅包含一个应用程序。假设我的域名为 myproject.com
,而我的应用名为 myapp
。我希望索引为 myproject.com /
而不是 myproject.com/myapp /
。
I have a django project in production, only including a single app at the moment. Let's say my domain is myproject.com
and my app is called myapp
. I want the index to be myproject.com/
not myproject.com/myapp/
.
这是我的 myproject / urls.py
的样子:
urlpatterns = patterns('',
url(r'^myapp/', include('myapp.urls')),
...
和 myapp.urls.py
:
urlpatterns = patterns('myapp.views',
url(r'^$', 'about'),
...
如何在myproject.com下提供该应用程序?
How can I make the app available under myproject.com? Or is that something you should be doing by a re-direct, e.g. in your .htaccess?
推荐答案
您必须重新定义您的 urls.py
到此
You have to redefine your urls.py
to this
urlpatterns = patterns('',
url(r'^$', include('myapp.urls')),
...
这将捕获对'/'的所有调用。
This will capture all calls to '/'.
这将启用 root中的所有URL。您必须编写 myapp.urls
,这样当
放入网络浏览器时它们才有意义。
This will enable all your urls from the "root".Then of course you have to write your myapp.urls
so that they make sense whenput in a webbrowser.
更新
所以在大声聊天之后:我想这就是你想要纯粹的代码方式
So after a big ol' chat: this is the thing I think you want pure codewise
在您的根目录 urls.py
url(r'', include('myapp.urls')),
myapp.urls
in your myapp.urls
urlpatterns = patterns('myapp.views',
url(r'^$', 'first_view'),
url(r'^evilCellphones/$', 'another_view'),
)
这里基本上发生的是,您将 myapp.urls
包含到
命名空间中,但是由于Django仍然加载它,所以它将包含新定义的网址。
从那里可以重新定义所有URL,并让它们遵循与 myproject相同的名称
空间。
What essentially happens here is that you include your myapp.urls
to nonamespace but since Django still loads it it'll include your newly defined urls.From there you can redefine all of your urls and have them get obey the same namespace as "myproject".
这篇关于应用程式网址到专案网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!