Relative path in settings.py in django - django

for various settings(MEDIA_ROOT,TEMPLATE_DIRS) in setting.py it is instructed to give absolute path.I have configured apache with mod_wsgi.I have a wsgi script in the folder named apache that redirects to settings.py.
import os
import sys
path = 'D:/Projects/Dan'
if path not in sys.path:
sys.path.append(path )
os.environ['DJANGO_SETTINGS_MODULE'] = 'Django.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
(don't misunderstand - Django is the name of my project.)
Okay my question is - now that we have imported the path to my project to system path in the wsgi script, isn't it more feasible that I give a relative path in settings.py,since that would make deploying easier.If I am wrong please tell me the standard procedure so that I can set all the path in one file other than 3 files(Apache - httpd.conf, mod_wsgi - django.wsgi, django - settings.py).

Use:
import os
this_directory = os.path.dirname(__file__)
and then:
absolute_directory = os.path.join(this_directory, 'relative.txt')
BTW, calling your project 'Django' is ill advised given that Django package itself is 'django'. You shouldn't rely on differences in case not causing confusion. In short, Python packages/module names should ever differ just by case. Your site project directory is treated as a Python package and thus the problem.

Related

Error running WSGI application, ModuleNotFoundError: No module named 'mysite'. Also not able to find Static files

I'm trying to deploy my website using pythonanywhere. I've given the correct path in wsgi file which is as follows (see code below). Still I'm getting a ModuleNotFoundError.
Also, it's not able to find Static files, however, they are at correct location.
Wsgi file code:
{
path = '/home/divyanshu964/portfolio/Personal_portfolio/'
if path not in sys.path:
sys.path.insert(0, path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'Personal_portfolio.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
}
Can someone help? Thanks.
Hello #DivyanshuRaghuwanshi to load static files you can set DEBUG=True inside settings.py but it's not a good practice to set DEBUG on in production so another way is to set your DEBUG=False and create STATIC_ROOT and configure it inside project root urls.py and than run python manage.py collectstatic command and than give path of your static root directory in pythonanywhere configuration
Apart from settings directly related to Django, there are some extra steps on PythonAnywhere.
wsgi file: you need to edit the file that is linked on the Web app page, not a file inside your project (check the docs here)
static files: set up static files mappings on the Web app page as well (check the docs here).

what's the choice should be done with django_wsgi and wsgi

My current version of django is 1.6.3
And i want to deploy django on my centos server with uwsgi and nginx.
There's a tutorial posted by someone who use 1.3's django that says it should create a file named django_wsgi.py inside my django project. I was wondering if this step could be ignored because i already have wsgi.py in my project at beginning. Are django_wsgi.py and wsgi.py the same thing? Just change the name along with the version upgrade.
#django.py
mport os
import sys
sys.path.append("/opt/www")
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
#wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "netmag.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
The project structure generated by startproject changed since 1.3. Before 1.4 release, you had to manually create a wsgi file. Since 1.4, Django automatically creates wsgi.py file in the project root.
Continue using wsgi.py that Django created for you.
A Django 1.6 already has a default WSGI configuration as you can see in your wsgi.py file, nothing else is needed. The instructions you mention are for legacy Django projects.
See https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ for more information on the Django WSGI config.

Django deployment, cannot import settings

I'm trying to deploy an application with django.
I've put my django_project directory in /home/XXX/websites/YYY.
The web server root is in /srv/http/YYY and it only contains a directory named static and an apache.wsgi file.
The content of apache.wsgi is as follow:
import os, sys
sys.path.append('/home/XXX/websites/YYY')
os.environ['DJANGO_SETTINGS_MODULE'] = 'YYY.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Whenever I reload apache modules and try to load a page, I got an import error :
ImportError: No module named YYY.settings
I don't understand. Since the settings.py file is in /home/XXX/websites/YYY/YYY/settings.py, why this don't work ?
I've manage to make this work by putting the django project in the web server root directory, but that wasn't the place where I wanted to put it.
Thanks for your help.

How do you specify which django version to use?

I am using ubuntu and I have installed django from the Ubuntu Software Center. For some projects I want to use the django cloned from the trunk instead of the default one. How can i do that ? Do I need to unistall the one provided by ubuntu?
Create a virtualenv for your django (with --no-site-packages) and activate it. Then install everything you need inside it.
No, simply make sure that the one you want to use shows up in an earlier directory in sys.path.
if wsgi in use
then set the path to the desired django installation in the django.wsgi file. eg
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path[:0] = ['/path/to/django/version/','/path/to/project/']
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If modpython then set the path in the apache configuration file
PythonPath "['/path/to/django/version','/path/to/project'] + sys.path"
The docs might help:
http://docs.djangoproject.com/en/dev/howto/deployment/modpython/

Problem setting up django on Apache

This is probably really simple. But I guess I'm too new to WSGI and Django to get it on my own. I have a brand new shiny Django project on a Ubuntu virtual machine hosted in /var/www/mysite. The project was created with
django-admin startproject mysite
I'm following a WSGI tutorial to get it set up, so I created an ./apache folder, inside of which I placed this file, django.wsgi:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I then added this config line to the Apache configuration:
WSGIScriptAlias / /var/www/mysite/apache/django.wsgi
When I try to hit the site, nothing is returned. The connection just hangs. This is in my access.log:
192.168.2.116 - - [15/Aug/2010:14:09:02 -500] "GET / HTTP/1.1" 500 639 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
So it's hitting the site. But someone isn't doing anything. There are no errors in the errors.log.
Anyone have any ideas?
The blatant mistake in original question is that:
os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'
would need to be:
sys.path.append('/var/www')
sys.path.append('/var/www/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
That is, Django settings module must be a Python package path, NOT an absolute file system path. The accepted answer doesn't even point that out and rather just gives another set of code to use with no explanation.
Further, you need to set Python module search path, ie., sys.path, so that Python can find your settings file listed in that environment variable.
Finally, if your browser was hanging you have done something else wrong as well, as what you did should have resulted in an error being returned and not the browser hanging. There would also have had to be an error in the Apache error log, so you are either looking in wrong place or don't know what to look for.
Aren't you missing path to your Django application? My app.wsgi has this:
import os, sys
sys.path.append('/usr/local/src/django-1.2') # django is outside default path
sys.path.append('/usr/local/src/django-photologue-2.2') # app i'm using
sys.path.append('/var/www/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# this is regular python import, so my settings are physically
# here: /var/www/mysite/settings.py
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()