How to get site's root path in Django

2,826 views
Skip to first unread message

yuanyun.ken

unread,
Oct 27, 2008, 11:24:49 AM10/27/08
to Django users
Hi, all great Django users and developers, I have a little problem.
To make @login_required work,
I have to configure those in settings.py:
root_url = '/root_url/'
LOGIN_URL = root_url + '/login/'
LOGOUT_URL = root_url + '/logout/'

But this is obviously not a good idea, as it couples with project's
root url.

I tried to use:
LOGIN_URL = '/login/'
or
LOGIN_URL = 'login/'

the first can not work, apache would try to access http://localhost/login/,
not http://localhost/root_url/login/
the second option would not work, when access http://localhost/root_url/dira/pageb,
this would lead to access http://localhost/root_url/dira/login

urls.py
(r'^login/$', 'django.contrib.auth.views.login'),
(r'^logout/$', 'django.contrib.auth.views.logout')

The following are my configuration in apache's httpd.conf
Alias /root_url/ D:/ws/django/myproject/
<Location '/root_url/'>
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE Regrroot_url.settings
PythonOption django.root /root_url
PythonDebug On
PythonPath "['D:/ws/django/myproject/'] + sys.path"
Options ExecCGI
Options +Indexes
Order allow,deny
Allow from all
</Location>

Any help is appreciated, and Thanks in advance.

Steve Holden

unread,
Oct 27, 2008, 11:54:11 AM10/27/08
to django...@googlegroups.com
yuanyun.ken wrote:
> Hi, all great Django users and developers, I have a little problem.
> To make @login_required work,
> I have to configure those in settings.py:
> root_url = '/root_url/'
> LOGIN_URL = root_url + '/login/'
> LOGOUT_URL = root_url + '/logout/'
>
> But this is obviously not a good idea, as it couples with project's
> root url.
>
>
But in Django your project's root URL is "/", which is why the default
settings.LOGIN_URL is "/accounts/login/".

> I tried to use:
> LOGIN_URL = '/login/'
> or
> LOGIN_URL = 'login/'
>
> the first can not work, apache would try to access http://localhost/login/,
> not http://localhost/root_url/login/
>
So what you are saying is that you aren't running Django mapped to the
root of your server's web address space?

> the second option would not work, when access http://localhost/root_url/dira/pageb,
> this would lead to access http://localhost/root_url/dira/login
>
> urls.py
> (r'^login/$', 'django.contrib.auth.views.login'),
> (r'^logout/$', 'django.contrib.auth.views.logout')
>
> The following are my configuration in apache's httpd.conf
> Alias /root_url/ D:/ws/django/myproject/
> <Location '/root_url/'>
> SetHandler python-program
> PythonHandler django.core.handlers.modpython
> SetEnv DJANGO_SETTINGS_MODULE Regrroot_url.settings
> PythonOption django.root /root_url
> PythonDebug On
> PythonPath "['D:/ws/django/myproject/'] + sys.path"
> Options ExecCGI
> Options +Indexes
> Order allow,deny
> Allow from all
> </Location>
>
> Any help is appreciated, and Thanks in advance.
If you don't want Django at the root of your web space then you will
have to take special pains to remap it. What's the reason you want your
project to appear at "/root_url/" rather than "/"?

regards
Steve

yuanyun.ken

unread,
Oct 28, 2008, 2:38:00 AM10/28/08
to Django users
Steve, Thanks for your expeditious reply.
because our apache server includes other apps, I can not deploy my app
to the root of the server.
and I am sure, many Django apps are not deployed to server's root too.
so there must be a way to handle this problem.

Graham Dumpleton

unread,
Oct 28, 2008, 5:19:06 AM10/28/08
to Django users


On Oct 28, 2:24 am, "yuanyun.ken" <yuanyun....@gmail.com> wrote:
> Hi, all great Django users and developers, I have a little problem.
> To make @login_required work,
> I have to configure those in settings.py:
> root_url = '/root_url/'
> LOGIN_URL = root_url + '/login/'
> LOGOUT_URL =  root_url + '/logout/'
>
> But this is obviously not a good idea, as it couples with project's
> root url.
>
> I tried to use:
> LOGIN_URL = '/login/'
> or
> LOGIN_URL = 'login/'
>
> the first can not work, apache would try to accesshttp://localhost/login/,
> nothttp://localhost/root_url/login/
> the second option would not work, when accesshttp://localhost/root_url/dira/pageb,
> this would lead to accesshttp://localhost/root_url/dira/login
>
> urls.py
> (r'^login/$', 'django.contrib.auth.views.login'),
> (r'^logout/$', 'django.contrib.auth.views.logout')
>
> The following are my configuration in apache's httpd.conf
> Alias /root_url/ D:/ws/django/myproject/
> <Location '/root_url/'>
>     SetHandler python-program
>     PythonHandler django.core.handlers.modpython
>     SetEnv DJANGO_SETTINGS_MODULE Regrroot_url.settings
>     PythonOption django.root /root_url
>     PythonDebug On
>     PythonPath "['D:/ws/django/myproject/'] + sys.path"
> Options ExecCGI
> Options +Indexes
> Order allow,deny
> Allow from all
> </Location>
>
> Any help is appreciated, and Thanks in advance.

When mounting at sub URL of a site, you shouldn't be adding the mount
point into anything in settings or in urls.py. For mod_python, the
only point you should need to added it is the PythonOption setting for
django.root. Thus, your settings for LOGIN_URL and LOGOUT_URL were
wrong when you were including root url in them.

Do note though that mounting at a sub URL possibly only works properly
if using Django 1.0, so make sure you aren't using an older version of
Django.

Graham

yuanyun.ken

unread,
Oct 28, 2008, 11:10:09 AM10/28/08
to Django users
Graham, thanks for your kindly help.

I know we don't need root url when configure url mapping. but it seems
that LOGIN_URL is somewhat special.

When I set LOGIN_URL = '/login/'
accessing http://localhost/root_url/login/ is ok,
but when I access some pages which need login first, I will got 404
error, and in apache's error log, it logs:
File does not exist: D:/Program Files/Apache Software Foundation/
Apache2.2.9/htdocs/login

and the only way to work is adding root url to LOGIN_URL.
LOGIN_URL = '/root_url/login/'

I can not figure out whether there are problems in my configuration,
or anything else.

Karen Tracey

unread,
Oct 28, 2008, 12:46:11 PM10/28/08
to django...@googlegroups.com

Sounds like:

http://code.djangoproject.com/ticket/8906

So (assuming you are not "levity") someone else has reported the same thing.  It sounds like there may be a problem in Django code here, but no one has yet investigated far enough to confirm that or suggest a fix.

Karen

yuanyun.ken

unread,
Oct 30, 2008, 6:24:02 AM10/30/08
to Django users
hi, Karen, Thanks for your reply.
I am sure I hit that problem, http://code.djangoproject.com/ticket/8906
and I wish some excellent gays have time to investigate, and provide a
fix.
For now, I would just include root path in LOGIN_URL and LOGOUT_URL
variable.

bruno desthuilliers

unread,
Oct 30, 2008, 12:27:41 PM10/30/08
to Django users


On 28 oct, 07:38, "yuanyun.ken" <yuanyun....@gmail.com> wrote:
> Steve, Thanks for your expeditious reply.
> because our apache server includes other apps, I can not deploy my app
> to the root of the server.

This won't fix http://code.djangoproject.com/ticket/8906, but there's
at least one possible workaround in the meantime : use a sub domain
(ie subdomain.yourdomain.tld).


Reply all
Reply to author
Forward
0 new messages