Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django in sub-directory

Tags:

python

url

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.

My IIS it's configure with an application under my default website called "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'),
]

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.

like image 675
Symon Avatar asked Feb 26 '26 16:02

Symon


1 Answers

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.

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

like image 140
knbk Avatar answered Feb 28 '26 06:02

knbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!