Whole this day I was trying to configure django on production server. I use mod_python. When I open http: //beta.example.com I see my site but http: //beta.example.com/admin and http: //beta.example.com/441/abc/ doesn't work:
Page not found (404)
Request Method: GET
Request URL: http://beta.example.com/admin
{'path': u'admin'}
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Page not found (404)
Request Method: GET
Request URL: http://beta.example.com/441/abc/
{'path': u'441/abc/'}
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
My urls:
from settings import MEDIA_ROOT
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# static files
url(r'^static/javascripts/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT + '/javascripts'}, name='javascripts'),
url(r'^static/images/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT + '/images'}, name='images'),
url(r'^static/stylesheets/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT + '/stylesheets'}, name='stylesheets'),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}, name='static'),
(r'^admin/', include(admin.site.urls)),
url(r'^/?$', 'content.views.index', name='root-url'),
url(r'^(?P<id>[0-9]{2,5})/(?P<slug>[a-z\-]+)/?$', 'content.views.show', name='show-url'),
)
Apache:
DocumentRoot "/var/www/django/beta.example.com/site"
<Location "/">
allow from all
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE site.settings
PythonOption django.root /
PythonDebug On
PythonPath "['/var/www/django/beta.example.com', '/var/www/django/beta.example.com/site'] + sys.path"
</Location>
<Location "/static" >
SetHandler none
</Location>
I have no idea what's wrong.
Not sure if that will solve your problem but in my site.conf for django I had to comment the line:
PythonOption django.root /
to make it work.
You really don't want to be using mod_python for deployment. I highly suggest moving to mod_wsgi for Django depoyment.
I'm just throwing this out there, but you have to enable the django admin middleware in your settings file. It's there but commented out right out of the box. If you've done that I don't have any idea what your problem is.
Related
I've been trying to setup an Apache (2.2.15) as a proxy with gunicorn and django (1.6.11) on a CentOS server (6.7), but I'm stuck on a problem regarding the static files. (I'm fairly new to django)
I have already looked at the great documentation that Django provides, as well as a few other stackoverflow's posts, but to no avail.
What I could check :
The problem doesn't seem to come from Django or the templates, as running the development server with 'DEBUG = False' and the option '--insecure' works fine.
It probably is a problem with the Alias and Directory block I added in my virtualhost, but I can't make sense of why it doesn't work.
Gunicorn shouldn't have any word to say in it, as it doesn't serve static files.
What my configuration looks like :
1) I got my project's static files into '/var/www/myproject/static' thanks to the 'collectstatic' command.
2) Then, I created the virtualhosts (where I think the problem is) :
(first one for the redirection to HTTPS)
<virtualhost *:80>
ServerAdmin user#domain.com
ServerName localhost
ServerSignature Off
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</virtualhost>
(second one for the actual work)
<virtualhost *:443>
ServerAdmin user#domain.com
ServerName localhost
ServerSignature Off
DocumentRoot /var/www/myproject
Alias /static/ "/var/www/myproject/static/"
<Directory "/var/www/myproject/static">
Order Allow,Deny
Allow from All
</Directory>
ProxyPass / http://127.0.0.1:8000/ connectiontimeout=150 timeout=300
ProxyPassReverse / http://127.0.0.1:8000/
ProxyPreserveHost On
ProxySet http://127.0.0.1:8000/ connectiontimeout=150 timeout=300
... here goes the SSL and Log configuration ...
</virtualhost>
After a service restart, my static files weren't loaded and I had a 404 error on their get. An 'httpd -S' didn't throw any error and the rest of the functionalities of the web interface are working great.
I also tried without the ending '/' for the '/static/' alias as it seemed to be a problem for some other people, tried to move the files directly under /var/www/myproject and have them accessed without an alias with the DocumentRoot...
If you want to have a look at the django settings.py (don't know if it's relevant, but some django guru could find something wrong there too) :
STATIC_ROOT = '/var/www/myproject/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__),'static',),)
As well as the templates :
{% load staticfiles %}
....
<link href="{% static 'bower_components/bootstrap/dist/css/boostrap.min.css %}" rel="stylesheet">
Urls.py of the project :
from django.conf.urls import patterns, include, url
from django.shortcuts import redirect
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
url(r'^$', lambda x: redirect('/Collecte/')),
url(r'^Collecte/', include('Collecte.urls', namespace="Collecte")),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
And the urls.py of the app (named 'Collecte'):
from django.conf.urls import patterns, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from Collecte import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^Collectes/Execution/รน', views.CollExecution, name='CollExecution'),
... quite a lot of them ...
)
# Commented this line from a suggestion, was present at start
#urlpatterns += staticfiles_urlpatterns
If you feel like any file is missing for the question to be relevant, just ask for it and I'll do my best :).
Alright,
I found the answer to my question in another stackoverflow thread that had a similar configuration (but with flask instead of django).
As Ajay Gupta said, I think I had a problem in the urls.py file of the project as I had to add 'static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)' (switched from using the staticfiles app django provides to this other way).
The other problem was in the apache virtualhost configuration, as I wanted my static files to be served by apache and not gunicorn or django. I was redirecting everything to the gunicorn server, even the requests to static files. So I had to add 'ProxyPass /static/ !' so that apache serves the files.
I don't know if it's the right way to do it, but it worked for me.
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
is a helper function that works only when
debug = True
So for instance just set debug = True again, restart your server and check
I have a problem with static css files.
I added to your application (in django 1.8) Page 404 - 404 page template and code in a file urls.py:
handler404 = 'django.views.defaults.page_not_found'
My STATIC_ROOT:
STATIC_ROOT = os.path.join(BASE_DIR, 'public_assets')
In settings.py file, I have the code:
Debug = False
ALLOWED_HOSTS = ['*',]
ALLOWED_HOSTS = ['localhost', '127.0.0.1',] - also doesn't work
After starting website, do not load styles and pictures.
Not sure this will work are not
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes':True}),
)
add above at the end of root urls.py
and run collectstatic
I'm using this guide to attempt to get this working. Basically, I'm exploring django 1.6 (with python 2.7.6 on Mac OS X Yosemite beta), still working with the stock development server. I'm trying to include a CSS file to override some styles in the admin area. I have a static folder in my project root. My settings.py is completely stock (that means I have DEBUG set to true and that I'm using django.contrib.staticfiles). Inspecting the source and request/response reveals that I'm calling for the CSS file at my expected path, but that I'm getting a 404 when attempting to load it. I also get a 404 when attempting to hit the CSS file directly in the browser. I've searched google and SO and have not, as of yet, been able to find an answer.
The requested CSS file:
http://*mysite*/static/admin/css/mysite-admin.css
The file system path to the CSS file:
*myprojectdir*/static/admin/css/mysite-admin.css
Yes it won't work because Django does not serve by default static assets, in order to have static assets served (mind though this should be the case only for local dev) in your main urls.py file add this to the top:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
And then at the end of your urls.py:
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + staticfiles_urlpatterns() + urlpatterns
Make sure you have defined STATIC_URL, MEDIA_ROOT and MEDIA_URL in your settings.py, for development STATIC_ROOT IS NOT needed.
I am deploying a Django project on apache server with mod_python in linux. I have created a directory structure like:
/var/www/html/django/demoInstall where demoInstall is my project. In the httpd.conf I have put the following code.
<Location "/django/demoInstall">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE demoInstall.settings
PythonOption django.root django/demoInstall
PythonDebug On
PythonPath "['/var/www/html/django'] + sys.path"
</Location>
It is getting me the django environment but the issue is that the urls mentioned in urls.py are not working correctly.
In my url file I have mentioned the url like:
(r'^$', views.index),
Now, in the browser I am putting the url like : http://domainname/django/demoInstall/ and I am expecting the views.index to be invoked. But I guess it is expecting the url to be only: http://domainname/ .
When I change the url mapping to:
(r'^django/demoInstall$', views.index),
it works fine. Please suggest as I do not want to change all the mappings in url config file.
Thanks in advance.
There's a fairly simple way around this using just django, without having to touch apache.
Rename your urls.py to something else, e.g. site_urls.py
Then create a new urls.py which includes that
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^django/demoInstall/', include('site_urls.py')),
)
This will ensure that all the url reversing continues to work, too.
That should be:
PythonOption django.root /django/demoInstall
Ie., must match sub URL in Location directive.
You shouldn't be using that prefix in urls.py.
The configuration below works fine on my remote host (same dir structure, same django), all admin media are served properly
settings
MEDIA_ROOT = '%s/static/' % FS_ROOT
STATIC_DOC_ROOT = '%s/static/' % FS_ROOT
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '%smedia/' % MEDIA_URL
urls
(r'^admin/', include(admin.site.urls)),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '%s/static' % FS_ROOT }),
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '%s/media' % FS_ROOT }),
django 1.2.0 # ubuntu 9.10, http://127.0.0.1:8084/ via runserver_plus
Admin media files are stored under /static/media/ in my project root dir and every static files/dirs under /static/. All statics are served fine, only the admin media are taken from the default django's admin media files. What am I forgetting and why it does affect the project only on my localhost? I've tried to everride /static/media/ path in the urls in various ways, but still nothing.
There are two solutions:
You can either set a hostname in ADMIN_MEDIA_PREFIX as suggested in this answer.
Or you can start the development server with the --adminmedia parameter as described in the django documentation.