子目录中的Django [英] Django in sub-directory

查看:79
本文介绍了子目录中的Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我构建django应用程序时,我将使用runserver命令来测试我的网站,但是在部署中,假设我将在以下网站发布我的网站:www.test.com/django。

When i build my django application, i'll use the runserver command in order to test my website, but in deploy, let's say that i'll publish my website under: www.test.com/django.

我的IIS是使用默认网站 django下的应用程序进行配置的。

My IIS it's configure with an application under my default website called "django".

我希望一切都会可以正常工作,但是django无法识别我的网址架构,即以下网址:

I'm expecting that everything will works fine, but django doesn't recognize my url schema, that is the following one:

urlpatterns = [
    # Examples:
    url(r'^$', app.views.home, name='home'),
    url(r'^contact$', app.views.contact, name='contact'),
    url(r'^about', app.views.about, name='about'),
]

,在这种情况下,我需要像这样修改urlpattern,以便通过www.test.com/django为应用程序提供服务。

and in this case, i need to modify my urlpatterns like this, in order to serve the application via www.test.com/django:

urlpatterns = [
    url(r'^(?i)django/', include([ #Application name
        # Examples:
        url(r'^$', app.views.home, name='home'),
        url(r'^contact$', app.views.contact, name='contact'),
        url(r'^about', app.views.about, name='about'),
    ])),
]

这是一个好方法吗?它正在工作,但是我不确定此解决方案的质量。

It's a good approach? it's working but i'm not sure about the quality of this solution.

推荐答案

解决此问题的首选方法是让您的网络服务器传递 SCRIPT_NAME wsgi变量。 Django在构建网址时会自动使用此变量作为前缀,而无需更改网址配置。我不熟悉IIS,因此无法告诉您如何执行此操作。它的确具有优势,因为脚本名是传递给Django而不是在其设置中配置的,因此您的代码完全与WSGI应用程序的实际挂载点无关。

The preferred method to fix this is to have your webserver pass the SCRIPT_NAME wsgi variable. Django will automatically use this variable as a prefix when constructing urls, without the need to change your url configuration. I'm unfamiliar with IIS, so I can't tell you how to do this. It does have the advantage that your code is completely agnostic to the actual mount point of your WSGI application, as the script name is passed to Django rather than configured in its settings.

或者,您可以设置 FORCE_SCRIPT_NAME 设置为 / django /

Alternatively, you can set the FORCE_SCRIPT_NAME setting to /django/.

这篇关于子目录中的Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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