子目录中的Django

编程入门 行业动态 更新时间:2024-10-14 02:22:31
本文介绍了子目录中的Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我构建django应用程序时,我将使用runserver命令来测试我的网站,但是在部署中,假设我将在以下网站发布我的网站:www.test/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/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/django为应用程序提供服务。

and in this case, i need to modify my urlpatterns like this, in order to serve the application via www.test/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

本文发布于:2023-11-01 13:21:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目录中   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!