Django URL resolution does not work when starting Apache / WSGI

When I run my application using the built-in Djangos server, everything works fine. But when I try to run through Apache and WSGI, the URL is no longer recognized, but it is in the urls.py file.

The error page I am getting is this:

Page not found (404) Request Method: GET Request URL: http://localhost/project/app/live/ Using the URLconf defined in project.urls, Django tried these URL patterns, in this order: ^media/(?P<path>.*)$ ^app/live/ The current URL, app/live/, didn't match any of these. 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. 

As you can see, the URL (app / live /) is directly in the URL patterns from the top-level urls.py file. There are also no errors in the Apache errors.log file.

My urls.py:

 from django.conf.urls.defaults import * from django.conf import settings urlpatterns = patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT }), (r'^app/live/', include('project.app.urls', app_name='live')), ) 

My WSGI file:

 import os import sys sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../..') os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings" import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() 

And finally, my Apache configuration:

 WSGIDaemonProcess project processes=2 maximum-requests=500 threads=1 WSGIProcessGroup project WSGIScriptReloading On WSGIScriptAlias /project /home/user/project/apache/project.wsgi 

EDIT:

After entering some debug output in RegexURLResolver, I saw that it was trying to resolve both app/live/ and project/app/live/ .

So, I changed the urls.py file to this:

 from django.conf.urls.defaults import * from django.conf import settings urlpatterns = patterns('', (r'^app/live/', include('project.app.urls', app_name='live')), (r'^project/app/live/', include('project.app.urls', app_name='live')), ) 

It works now.

+4
source share
4 answers

WSGIScriptAlias /django trailing slash on WSGIScriptAlias /django worked for me

+2
source

Add a trailing slash to the URL path prefix in your Apache configuration:

 WSGIScriptAlias /project/ /home/user/project/apache/project.wsgi 
+1
source

Solution 1, edit apache configs:

 WSGIScriptAlias / /home/user/project/apache/project.wsgi 

Solution 2, edit your urls.py:

 from django.conf.urls.defaults import * from django.conf import settings urlpatterns = patterns('', (r'^/project/media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT }), (r'^/project/app/live/', include('project.app.urls', app_name='live')), ) 

Also, you probably don't want to use static content through django if using apache:

 Alias /media /path/to/media/root <Location /media> SetHandle None </Location> 
+1
source

I hate to spice up the old thread, but I fixed this problem by making sure my apache2 configuration wrapped the python path in quotes, for example:

 WSGIScriptAlias / /home/user/django_project/wsgi.py WSGIPythonPath "/home/user:/home/user/.virtualenvs/djangodev/lib/python3.4/site-packages" 

I am using Django 1.9, python3.4 and virtualenv, and this tutorial .

UPDATE: Scratches that I'm in maroon and didn't read well enough to see that you need to use mod_wsgi in daemon mode to avoid having to restart Apache every time I make changes.

 WSGIDaemonProcess example.com python-path="/home/user:/home/user/.virtualenvs/djangodev/lib/python3.4/site-packages" WSGIProcessGroup example.com WSGIScriptAlias / /home/user/django_project/wsgi.py process-group=example.com 
+1
source

Source: https://habr.com/ru/post/1343166/


All Articles