質問

I recently came across a very strange problem I could not resolve. I am running django with mod_wsgi and apache, and the problem is that

www.example.com/subdir

is not being directed to

www.example.com/subdir/

urls.py looks like this:

import os
from django.conf.urls.defaults import *
from fileupload.views import PictureCreateView, PictureDeleteView

urlpatterns = patterns('',
    (r'^$', PictureCreateView.as_view(), {}, 'upload-new'),
    (r'^delete/(?P<pk>\d+)$', PictureDeleteView.as_view(), {}, 'upload-delete'),
    (r'^fileupload/media/(.*)$', 'django.views.static.serve', 
    {'document_root':os.path.join(os.path.abspath(os.path.dirname(__file__)),'media')}),

)

It is kinda important for me to put a trailing slash at the end. I tried putting .htaccess in the folder, but then it only works with firefox and not google chrome.

Can you suggest a way to do this with django, or is this a problem with apache and not django

EDIT 1: APPEND_SLASH is not set to false.

Also httpd.conf also has this:

<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName example.cm

WSGIScriptAlias /subdir /var/www/html/cloudcv/apache/django.wsgi
<Directory /var/www/html/cloudcv>
  Order allow,deny
  Allow from all
</Directory>
</VirtualHost>

settings.py has this:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'fileupload.urls'
APPEND_SLASH = true

Also to make things more clear, the append slash is working fine when I put like r'^new$' instead of r'^$' and the file directory structure is like this:

- /var/www/html/cloudcv/
    + apache
    -fileupload
         urls.py
         views.py
         <other files>
    settings.py
    <other files>

here "other files" means other files of the directory

UPDATE 1: orokusaki pointed out a new error in the upload-delete url and he updated his answer to correct. Just pointing that out because it might be helpful to others who reach this answer.

役に立ちましたか?

解決

This is actually an artifact of modern browsers' attempts to beautify HTTP. If you copy the URL from the URL bar (or view the HTTP headers), you'll likely see that the trailing slash is indeed there.

Update

I wasn't looking closely enough your urls.py. You just need to do this:

urlpatterns = patterns('',
    (r'^$', PictureCreateView.as_view(), {}, 'upload-new'),
    (r'^delete/(?P<pk>\d+)/$', PictureDeleteView.as_view(), {}, 'upload-delete'),
    (r'^fileupload/media/(.*)$', 'django.views.static.serve', 
    {'document_root':os.path.join(os.path.abspath(os.path.dirname(__file__)),'media')}),

)

Note the / added to the upload-delete URL (do the same for any other views you wish to end in a slash). The way APPEND_SLASHES works is better documented in the CommonMiddleware docs than it is in the settings docs. The gist of it is: if A) request.path doesn't match any URL pattern in your application, and B) request.path + '/' does match a URL pattern in your application, Django will redirect to the latter.

Your the upload-delete URL wouldn't have matched /delete/123/ even if you manually typed it in manually, since the extra / wouldn't match the regexp.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top