django site doesn't see urls.py - django

I just moved my site to an actual apache server (was developing locally before) and the site can't seem to find the urls.py file. basically what happens is that the homepage works, which is weird in itself considering that if i go to any url, e.g. website/about/, i will get a 404 error with text {'path': u'about/'}.
I tried ROOT_URLCONF set to mysite.urls and just urls, and if i move the urls.py it will continue to behave the same way.
I don't know if its related but I also can't seem to access my site media folder, it seems as though the server is still reading it in its old path, but the settings.py file is correct. (tried restarting apache, rebooting server, etc..)

I would be more worried about it not finding the media directory, that's pure apache. If that part of the equation isn't working, nothing else will. Work with apache's httpd.conf until you can browse to the media directory correctly first.
Update:
I copied in my working conf file and substituted your values. Your django.root might need to be "" or not set at all, as I've found that it shouldn't end with a /:
<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE fikdusite.settings
PythonOption django.root ""
PythonDebug On
PythonPath "['/django_apps/', '/django_apps/fikdusite/'] + sys.path"
</Location>
And make sure that the .profile of the user that apache runs your site as, has:
export DJANGO_SETTINGS_MODULE='fikdusite.settings'
export PYTHONPATH=$PYTHONPATH:/django_apps:/django_apps/fikdusite

First, don't use mod_python, use mod_wsgi.
Secondly, don't forget that you need to restart Apache every time you make a code change in Django.

Related

my website urls via apache2 dont work

my first question for this site, i hope it goes well!!
I have, ubuntu, apache2, python, django and mod_python.
All is installed properly.
I have created a website project which works properly when i run it locally.
But i cant get it working the same way on apache.
I can access my website project directories, but i cant access my website projects URL's
I think this has something to do with incorrectly configuring my directives in the httpd.conf file. Also when i type the server name in the web browser i get a server not found
a quick rundown:
My project lives in /home/jamie/mysite
django, apache, modpython on root directory
in /etc/apache2/sites-available/http.conf i have:
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
ServerName www.example.com
DocumentRoot /home/jamie/mysite
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Location "/mysite">
SetHandler python-program
PythonHandler django.core.handlers.modpython
#removed line -PythonHandler mod_python.publisher- didnt work#
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonPath "['/home/jamie/', '/usr/local/lib/python2.6/dist-packages'] + sys.path"
PythonAutoReload On
PythonDebug On
</Location>
</VirtualHost>
ANybody that can help me i will give 1 BILLION DOLLARS to
ok akonsu cheers for this.
http//localhost/templates points to my templates folder and shows all the files and subdirectories. http//localhost/templates/homepage.html will show the homepage.html located in the templates folder with all the ugly django tags that go with it.
I want run my website app on apache the same way as if i ran it locally, via the urls.
example. http//localhost/homepage would point directly to the file homepage.html which is located in the templates folder as this is how it is set out in the urls.py file and would not show the ugly django tags.
If i do type in http//localhost/homepage via the apache server i get the url /homepage does not exist on this server
The django book tells me to point DJANGO_SETTINGS_MODULE to my apps settings file, which i have done 'DJANGO_SETTINGS_MODULE mysite.settings' The settings file points to the urls file which points to the views file which in turn renders with template files and so on and so forth. Thus if i typed http//localhost/homepage it should work as homepage has been configured properly in my urls.py file. I believe i have done what they have asked but still no luck. Either im getting the DJANGO_SETTINGS_MODULE part wrong or starting with /localhost is wrong.
I dont know what difference this makes but if i change the servername in the httpd.conf file to say www.blabla.com it wont throw an error when i restart apache server, meaning it's configured right. But when i type www.blabla.com in the browser i get an error saying this site does not exist.
try removing PythonHandler mod_python.publisher

Serving static files on a webserver in Django

I am setting up my site on an external server (AWS). Everything seems to be working except for the static files (CSS and images).
My project is set up like so --
/var/www/
djangoapps
myproject
settings.py, apps, etc.
djangotemplates
myproject
HTML files
/var/www/html
media
static
Images & CSS files
In the httpd conf file I have --
<Location /mysite>
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
SetEnv PYTHON_EGG_CACHE "/var/cache/www/pythoneggs"
PythonDebug Off
PythonPath "['/var/www/djangoapps'] + sys.path"
</Location>
<Location "/media/">
SetHandler None
</Location>
And in settings.py --
STATIC_URL = '/myproject/static/'
STATICFILES_DIRS = ('/var/www/html/media/static/',)
When I load the page, the templates are working, and the URL to the image files is 'correct' (e.g., background: url("/myproject/static/email.jpg"). However, the images are not loading. What do I need to change so that the images and CSS will load properly?
Have you seen the official docs on serving static media with mod_python? I suspect you need a similar SetHandler for your static files that you do for your media files.
Also, mod_python is deprecated.
You need to add an alias for your static files to be served from. Something like:
Alias /myproject/static/ /var/www/djangoapps/myproject/media/static
This way Apache will serve the static files instead of asking django to handle it.

URLs redirection problems deploying a working django app in apache (via mod_wsgi)

I've a django app that works perfectly under the django development server. I'm trying to deploying it in apache2.2 using the mod_wsgi and I have errors.
In the httpd.conf file I "mounted" my app under the root /myapp using:
WSGIScriptAlias /myapp /home/path_to_my_app/apache_conf/django.wsgi
I've followed the standard indication to prepare django.wsgi. Now, I can reach the main page template of my app but it seems to have redirection errors. The "/myapp" root is not inserted automatically on ULRs redirection requests. Request from the main page template to some modules in views.py via urls.py cannot be correctly delivered.
apache error log:
127.0.0.1 - - [21/Feb/2011:16:11:44 +0100] "GET /myapp/ HTTP/1.1" 200 1795
127.0.0.1 - - [21/Feb/2011:16:11:46 +0100] "GET /api/dir HTTP/1.1" 404 205
"/api/dir" doesn't exist, it should be matched from a pattern in urls.py, but id doesn't
I've spent more than 2 days with it, please can somebody help?
It appears you are missing some things from your apache config:
In the working config for my server I have:
# tell apache where to find the wsgi script.
WSGIScriptAlias / "/home/path_to_my_app/wsgi/my.settings.wsgi"
# turn on auto-reload in WSGI
WSGIScriptReloading On
WSGIReloadMechanism Process
# assign a process to a process group
WSGIDaemonProcess djangoapps processes=10 threads=1 maximum-requests=500 display-name=my-wsgi
WSGIProcessGroup djangoapps
Your WSGIScriptAlias line is correct but you need to tell wsgi how you are going to run your application.
You are most likely getting 404's because there is not a WSGIDaemonProcess line to tell the WSGI handler how to work with your process.
http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives might be a helpful reference.
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.
Actually it is a bit more complicated than just that.
There are some times when you need absolute in-app urls, because you don't want to reach an url which is relative to the current url.
e.g. you have a global menu in a main layout template (i.e. a template which is "extend"ed by each other template in your project): you cannot have relative urls like "home", "contact", "about", "blog" because if you are in /yourapp/blog/entries/2014/02/06/add (let's say such url exist) and you click any menu item, you would go to (say, for "home") /yourapp/blog/entries/2014/02/06/add/home instead to /home, which you would expect.
The ugly solution of virtual hosts is good here (in Java, Struts 2 has an option to resolve urls for "actions" (i.e. named urls / entry points) including the deploy context directory as deployed in the web container (e.g. tomcat)) ... don't know it there's any "context" setting you can guess (perhaps it's possible to return the current "WSGIScriptAlias" key as an environment variable). After that, using back the initial slash for urls ("/home").

Have problem with deploying django using apache

Hello I am having a problem with Django trying to get it deployed on a server using apache. for some reason when I write this information in my httpd.conf file.
<location "/mysite">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonOption django.root /mysite
PythonDebug off
PythonPath "['/user/local/src/djcode','/user/local/src/djcode/mysite'] + sys.path"
</Location>
I get this error when I restart apache.
Syntax error on line 3 of /etc/apache2/httpd.conf:
Invalid command 'PythonHandler', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
Any ideas?
this error is pretty much self explanatory (second line is important to you):
Invalid command 'PythonHandler', perhaps misspelled
or defined by a module not included in the server configuration
PythonHandler is a command unknown by apache. it's a "module command" known by mod_python. so if missing it says invalid command (OR defined by module not in...).
so make sure mod_python is installed.
you might know it anyways, but just follow this document http://docs.djangoproject.com/en/dev/howto/deployment/modpython/
Also, i recommend you use mod_wsgi instead of mod_python. See deprecation warning on top of this page: http://docs.djangoproject.com/en/1.2/howto/deployment/modpython/
Support for mod_python will be
deprecated in a future release of
Django. If you are configuring a new
deployment, you are strongly
encouraged to consider using mod_wsgi
or any of the other supported
backends.
python_mod is not installed or not configured in httpd.conf.
If you have installed it, then add this line below in httpd.conf:
LoadModule python_module modules/mod_python.so

safely hosting a django project over apache using centos

Error can be seen at: http://djaffry.selfip.com:8080/
I had a project working great, but I had all the files under /var/www/ and with my limited understanding it's bad, according to django's site:
"If your background is in PHP, you’re probably used to putting code under the Web server’s document root (in a place such as /var/www). With Django, you don’t do that. It’s not a good idea to put any of this Python code within your Web server’s document root, because it risks the possibility that people may be able to view your code over the Web. That’s not good for security.
Put your code in some directory outside of the document root, such as /home/mycode."
So I went to /home/tipu/stuff/ and executed django-admin.py startproject twingle. Then I went to apache and did
<VirtualHost *:8080>
ServerName tweet_search_engine
DocumentRoot /home/tipu/stuff/twingle/
</VirtualHost>
<Directory /home/tipu/stuff/twingle>
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE settings
PythonOption django.root /home/tipu/stuff/twingle
PythonDebug On
PythonPath "['/home/tipu/stuff/', '/home/tipu/stuff/twingle/'] + sys.path"
</Directory>
Now I am getting a 403 Forbidden error.. any idea what I'm doing wrong? I'm newer to Linux (CentOS) and django, so I could be over looking some very simple things.
This is almost certainly just an access rights issue. The Apache user needs rights to access all the directories in the path to your project - home, home/tipu, home/tipu/stuff, home/tipu/stuff/twingle, and so on. You'll need to find out what user Apache is running as, and grant read rights to those directories.
As Ignacio suggests, /srv is probably a better place to put this - but the same rights issues still apply.
Well, under /home is not the right place, thanks to SELinux. Put the app under /srv instead.