Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting Django root to admin panel

I'm trying to redirect the Django site root to the Django admin panel, and I've mostly got it up and running. However, I've noticed that unless there's a trailing slash in the project URL, the links on the page don't include the project folder, and instead try to find the apps in the site root.

So, say I've got a project foo, and an app bar. If you visit http://server/foo/ (with a trailing slash), everything works fine, and the links on the page go to http://server/foo/bar. However, if one visits http://server/foo, the generated links go to http://server/bar instead, which generates a 404 error.

If I set the WSGIScriptAlias to point to /foo/ instead of /foo, it would give a 404 error if I navigated to /foo. I tried to force a trailing slash in the Apache conf with Redirect, but I end up generating a recursive redirect (http://server/foo//////...). I haven't yet tried using a .htaccess file, but I suspect the same thing might happen.

I tried the same thing in urls.py, however:

url(r'^$', redirect_to, {'url': '/'}), # goes to server root http://server/
url(r'^$', redirect_to, {'url': 'foo/'}), # goes to http://server/foo/foo
url(r'^$', redirect_to, {'url': '/foo/'}), # infinite redirect

I also tried simply appending a slash to all the Django urls like so:

url(r'^(.*)/', include(admin.site.urls))

But it fails to match anything at all in the project root folder (although if you navigate to the app, that seems to work OK).

I'm using Apache 2.2 with mod_wsgi, here is the configuration:

Alias /static "C:/DJANGO~1/apps/django/django/contrib"

<Directory 'C:/DJANGO~1/apps/django/django/contrib'>
  Order allow,deny
  Allow from all
</Directory>

WSGIScriptAlias /foo "C:/Djangostack/apps/django/scripts/django.wsgi"

<Directory 'C:/Djangostack/apps/django/scripts'>
  Order allow,deny
  Allow from all
</Directory>

And this is the urls.py that mostly works:

urlpatterns = patterns('',
  url(r'^', include(admin.site.urls)),
)

I've made sure APPEND_SLASH is set to True, but it doesn't seem to work in the root project folder.

like image 365
Hannele Avatar asked Nov 14 '22 10:11

Hannele


1 Answers

Try setting APPEND_SLASH to True in your settings.py. I had a similar problem and this fixed it for me. It's supposed to default to True but I had to set it explicitly.

like image 103
aganders3 Avatar answered Dec 25 '22 11:12

aganders3