Obtaining void template passing from django development server to apache server
I've a django app that works perfectly under the django development server. Now I'm trying to run it under apache2.2 using the mod_wsgi.
In the httpd.conf file of apache I've added:<IfModule wsgi_module>
WSGIScriptAlias /index my_path_to_wsgi_modules/django.wsgi
</IfModule>
and the django.wsgi module contains all the basic configuration as explained in django documentation: http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
Unfortunately when I run the server and try to access the main page I obtain the template page but without variable data in it. The server log says:[Fri Feb 18 13:50:33 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/api, referer: http://127.0.0.1/example/
As I said before the strange thing is that the same code works perfectly under django development server. I'm new in programming web app, please, can anybody help?
My django.wsgi file looks like this:
import os import sys
from os.path import sep
basepath = '/home/example/WorkSpace/examplews/src'
sys.path.append(basepath)
sys.path.append('/home/example/WorkSpace/examplews/src/examplews')os.environ['DJANGO_SETTINGS_MODULE'] = 'examplews.settings'
import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
And my httpd.con file, like this:
ServerRoot "/usr/local/apache2"
Listen 80
LoadModule wsgi_module modules/mod_wsgi.so
User apache
Group apacheServerAdmin example@domain.com
DocumentRoot "/usr/local/apache2/htdocs"
Options FollowSymLinks AllowOverride None Order deny,allow Deny from all
<Directory />
</Directory>
Options Indexes FollowSymLinks
<Directory "/usr/local/apache2/htdocs">
AllowOverride None
Order allow,deny
Allow from all
</Directory>
DirectoryIndex index.html index.php index.sh default.jsp
<IfModule dir_module>
</IfModule>
Order allow,deny Deny from all Satisfy All 开发者_Go百科
<FilesMatch "^\.ht">
</FilesMatch>
ErrorLog "logs/error_log"
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
<IfModule logio_module>
</IfModule>
CustomLog "logs/access_log" common
</IfModule>
<IfModule alias_module>
</IfModule>
<IfModule cgid_module>
</IfModule>
WSGIScriptAlias /main /home/example/WorkSpace/examplews/src/examplews/apache_conf/django.wsgi
<IfModule wsgi_module>
</IfModule>
<Directory "/home/example/WorkSpace/examplews/src/examplews/apache_conf">
Order allow,deny Allow from all</Directory>
DefaultType text/plainTypesConfig conf/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz
<IfModule mime_module>
</IfModule>
SSLRandomSeed startup builtin SSLRandomSeed connect builtin
<IfModule ssl_module>
</IfModule>
Finally I found the error. It was not connected to httpd.conf file but to how URLs are specified both to django urls.py file and to templates.
As I mounted myapp in this way:
WSGIScriptAlias /myapp my_path_to_wsgi_module/django.wsgi
I was believing that URLs specified in django template files should carry an initial slash, like this:
'/api/dir'
It results that in this way the application works only on django development server but not on apache.
Instead if you use URLs without initial slash like:
'api/dir'
The app works correctly both on django development server and on apache!
You must avoid using starting slashes even on pattern matching of django urls.py file:
like this: (r'^api/dir$', 'available_services')
and NOT like this: (r'^/api/dir$', 'available_services')
Maybe this is an obvious thing for expert django users but if you're novice like me, this can make you loose a certain amount of time because it's a hard problem to be detected.
精彩评论