Configure django in a subdirectory

What I have:

Apache http server with 4 virtual servers with distinc domains:

www.mydomain.com points to /var/www/mydomain/htdocs (php code) 

and it works. All other domains are irrelevant.

On another machine, I developed a django project with two applications, and it works well with the django buj-in server.

What I need:

 www.mydomain.com pointing to /var/www/mydomain/htdocs (php code) www.mydomain.com/recsys pointing to /opt/repsys (django code) 

I want to save the PHP root code and install django inside / repsys.

I can put my django code in the root directory, but when I try to paste inside / repsys, I have two problems:

  • I had to put / repsys in all the urls of my settings.py file (this is not a problem, but it is strange for me).
  • And I have this error from django code:

    Reverse for 'ajax' with arguments' (u'viewproject ',)' and keyword arguments' {} 'not found

I see that this is due to changing the url (now it includes / repsys), and django does not know how to cancel this url, but I have no idea how to fix it ...

My django: 1.5.1

Here is my wsgi.py

 import os, sys sys.path.append('/opt/repsys') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "repsys.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 

and my apache conf file for this virtual server:

 LoadModule wsgi_module modules/mod_wsgi.so WSGISocketPrefix /var/run/wsgi #WSGIPythonPath /opt/repsys/ <VirtualHost *:80> WSGIDaemonProcess repsyssite display-name=%{GROUP} WSGIProcessGroup repsyssite ServerAdmin blahhh@gmail.com ServerName www.mydomain.com DocumentRoot "/var/www/mydomain/htdocs" WSGIScriptAlias /reprep /opt/repsys/repsys/wsgi.py Alias /reprep/static /opt/repsys/static_serve Alias /reprep/media /opt/repsys/media Alias /admin_media /home/myuser/Django-1.5/django/contrib/admin/media <Directory opt/repsys> Order allow,deny Allow from all </Directory> # Logfiles ErrorLog /var/www/logs/glob/error.log CustomLog /var/www/logs/glob/access.log combined </VirtualHost> 

My main url.py includes:

 url(r'^report/', include('report.urls', namespace="report")), 

And my /url.py report includes:

 url(r'^(?P<fn>\w+)/ajax/$', views.ajax, name='ajax'), 

And here is the line that works when I put my project in the root directory, but does not work when it is inside the folder:

 $('#viewreportdiv').load("{% url 'report:ajax' 'viewproject' %}", {proj: id}); 

Can someone point out how to fix this?

+4
source share
1 answer

Not sure if you are still looking for an answer to this question, but I finally got this job, so I thought I'd post it here.

I was able to solve this using the article that Graham Dumpleton did at http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html

At the end, my /etc/httpd/conf.d/django.conf file looked like this

 LoadModule wsgi_module modules/mod_wsgi.so WSGIDaemonProcess mysite python-path=/var/www/html/mysite:/var/www/html/mysite/venv/lib/python2.7/site-packages WSGISocketPrefix run/wsgi WSGIScriptAlias /mysite /var/www/html/mysite/mysite_www/wsgi.py process-group=mysite Alias /static/ /var/www/html/static/ Alias /docs/ /var/www/html/mysite/docs/_build/html/ <Directory /var/www/html/mysite/mysite_www> <Files wsgi.py> Allow from all Order deny,allow </Files> </Directory> 

Here I am using the Apache subdir specified through WSGIScriptAlias , instead of using <VirtualHost> .

From there, I can get to my php pages via: http://myserver.com/ and to the django vis website http://myserver.com/mysite/ .

Please note that I had to use trailing slash when viewing in Django (1.7), otherwise I would get to the main page, but the return URLs in my template will not be processed properly to get to the child pages.

0
source

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


All Articles