How to deploy a Django site using a different version of Django on Heilohost - django

I struggled with this for quite a while so I figured I'd Q&A it to save someone the headache over a relativity simple solution.
Heilohost comes with Django 1.3, Can I install my own version (Django 1.4)?

Edit:
Extended walk-through
Uploading your site
Create a new subdirectory in the public_html directory under your account root. Name this subdirectory the same name as your Django project. Now, upload your Django project to the specified subdirectory through either FTP or the cPanel File Manager. Do the same thing for your apps
It should look like this:
home/
+ <user_name>
+ public_html
- <project_name>
* __init__.py
* settings.py
* urls.py
* ...
- <app_name>
* __init__.py
* views.py
* models.py
* ...
- media
- templates
- static
Running Django 1.4
HelioHost comes installed with Django 1.3, which can be kind of obnoxious if you've developed everything in 1.4. Luckily, it's a pretty easy problem to solve. You'll need to use Filezilla or something to do this.
Go download the Django source files from https://www.djangoproject.com/download/1.4/tarball and unzip it. Then using filezilla transfer the directory Django-1.4/django into your public_html folder
Next, create a dispatch file (dispatch.wsgi) to handle your project, and put it in /home/<user_name>/public_html/<project_name> In order to run 1.4 you'll have to set it up like this:
import os, sys
sys.path.insert(0,"/home/<user_name>/public_html")
os.environ['DJANGO_SETTINGS_MODULE'] = '<project_name>.settings'
os.environ['PYTHON_EGG_CACHE'] = '/home/<user_name>/.python_egg_cache'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
"/home/<user_name>/public_html" has to be inserted into the front of your sys path so that your uploaded copy of django is the one that's used.
Make its CHMOD permissions 755
Follow the rest of the steps heilohost outlines and you're good to go

Related

deploying Django - Dreamhost

I am neewbie with django and I am stuck trying to deploy my project on the server ( a shared Dreamhost server). I have followed the tutorials I found but it doesnt work. My project structure is:
/usr/mydomain.com
- public
* media
* static
- project
* __init__.py
* manage.py
* settings.py
* urls.py
* views.py
- tmp
- django-setup.py
- manage.py
- passenger_wsgi.py
- passenger_wsgi.pyc
It only works on my domain.com/admin, but I have a simple 'hello world' view to try but it isnt showing. I get only an HTTP 404 when I access my domain...
EDIT
I have installed Python 2.7 and Django 1.5 . Now I get a 500 Internal Server Error.
This is how I do it, with Django 1.5, on DreamHost:
A virtualenv dedicated to my project is in ~/virtualenv/myproject
My Django project is in ~/projects/myproject, with setup:
The database file is in the project root, named sqlite3.db
STATIC_ROOT is set to os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'static'), that is, the static directory in the project root.
When the static files are updated in the project, I have to run python manage.py collectstatic
I have per-environment settings in ~/projects/myproject/myproject, named prod_settings.py, beta_settings.py, and so on.
My site is in ~/sites/www.myproject.com, the layout inside:
myproject -- symlink to the Django project
sqlite3.db -- symlink to the database file in the Django project
public/static -- symlink to the STATIC_ROOT defined in the Django project
tmp/restart.txt -- touch this file to have Passenger reload the site settings
passenger_wsgi.py -- the Passenger configuration
Create passenger_wsgi.py like this:
projectname = 'myproject'
virtualenv_root = '/home/jack/virtualenv/' + projectname
import sys
import os
INTERP = os.path.join(virtualenv_root, 'bin', 'python')
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
sys.path.append(os.path.join(os.getcwd(), projectname))
os.environ['DJANGO_SETTINGS_MODULE'] = projectname + '.prod_settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Create prod_settings.py like this:
from myproject.settings import *
DEBUG = False
TEMPLATE_DEBUG = DEBUG
ADMINS = (('My Project', 'info#myproject.com'), )
MANAGERS = ADMINS
DATABASES = {} # Appropriately for your production environment
SECRET_KEY = "..." # Your secret key
ALLOWED_HOSTS = ["myproject.com", "www.myproject.com"]
For Django 1.4 or earlier, you need to make some minor modifications, but the same idea works. I've been using variations of this setup since Django 1.2.
I setup virtualenv like this:
# install pip and virtualenv in my home directory
easy_install --user pip
pip install --user virtualenv
# create a virtualenv dedicated to my django project
mkdir ~/virtualenv
virtualenv --distribute ~/virtualenv/myproject
# activate the virtualenv, install django and all project dependencies
. ~/virtualenv/myproject/bin/activate
cd ~/projects/myproject
pip install -r requirements.txt
Notice that after activating the virtualenv, you don't need the --user flag anymore when you install packages with pip. When a virtualenv is active, all packages are installed in that virtualenv.
About requirements.txt:
Create it in your development like this: pip freeze > requirements.txt
Edit it, leave only the packages that your project really really needs. It's better to remove too much and add back later as needed.
Note that virtualenv is not necessary, but recommended. You can do without by setting INTERP in your passenger_wsgi.py to /usr/bin/python. virtualenv is useful to have multiple Django sites with different dependencies on the same account. It is also useful when you want to upgrade Django for your site, so that you can test the upgrade on beta.myproject.com without disrupting traffic on your main site.
Finally, if you are using shared hosting, DreamHost support encourages using different user accounts for each website, but I don't know what difference that makes. Be careful with heavy operations, for example large data imports, because if the process uses much memory, it can get killed by DreamHost. The memory ceiling is unspecified, but it's quite low. So if your site needs to do some heavy operations, you need to make such operations fault tolerant, in order to resume after killed.

ImportError when using Haystack 2.0.0 with Django 1.5 and Gunicorn WSGI

I use django-haystack 2.0.0 to index my site, and it has been working great until I upgraded to Django 1.5 and started using the WSGI interface. If I just use the django_gunicorn command it works great, but the Django documentation "highly recommends" I use the gunicorn command.
When I start my site with the gunicorn command, Haystack throws the following error on any page load:
ImportError: cannot import name signals
I have no problems importing signals from the Django or Python shells. I use virtualenv and install all packages locally inside that environment. My wsgi.py file looks just like the default one in the django admin, except that I add the local path to the python path as such:
path = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2])
if path not in sys.path:
sys.path.append(path)`
Any help you could provide would be very appreciated, thank you!
I don't use gunicorn, but I had the same problem when I used the HAYSTACK_SIGNAL_PROCESSOR setting to point to a custom class that I wrote. That class imported one of my models, which eventually propagated up the import chain, to import my settings module, thus causing a circular import.
When using a setting such as HAYSTACK_SIGNAL_PROCESSOR that points to a class, make sure that class standsalone, and doesn't import either directly or indirectly the Django settings file.

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.

admin site looks different in production environment

so I'm building a basic site to learn django, and i am currently setting up the admin site. when launch the site through
python manage.py runserver
i can go to the website and it looks pretty just like this. http://cl.ly/2Z3A2n0I1E140X3k180h
but when i load the site through my apache web server it looks like this. http://cl.ly/3H3r1G2D193p462t3641
can anyone help me?
it looks to me like the admin site is not able to load the proper template on the live site. but i did not setup any special template. and when i look at the permissions of the template folder it seems to be fine... maybe not. any help?
Ok, sorry for the long story:
First, find out where your django installation rests:
in the CLI:
>>> import django
>>> django
<module 'django' from '/usr/local/lib/python2.6/dist-packages/django/__init__.pyc'>
The this is the path (except for the __init__.pyc, of course) to your Django installation.
Now, in your media directory, you could create a symbolic link to the directory that the admin media is located in (this way you will not have to copy the files to your media directory).
Assuming that you are in your media directory use this command to create dynamic link to the admin media directory (the first argument to ln -s should of course be your django path that we got retrieved earlier):
ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/ admin
Now in your settings.py you can use something like this:
MEDIA_URL = 'media'
ADMIN_MEDIA_PREFIX = '/media/admin/
You will also have to add this directive to your Apache config:
Alias /media/ /full/path/to/your/django/site

google app engine (python): ImportError no module named django

So I'm trying to use the django 1.1 template engine with the google app engine web app framework, from here. This is on Ubuntu Jaunty, I've made sure that the PYTHONPATH contains the location of Django-1.1.1 yet I'm getting this 'ImportError: No module named django' error when it tries to execute the use_library() line below. Again, could somebody help me? I'm stumped.
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.1')
Came up with the following solution:
Get django 1.1 and put it under your project root.
Add an empty file "non_gae_indicator" to your project root folder.
Add django and non_gae_indicator to your app.yaml skip_files element:
skip_files:
- ^(.*/)?app\.yaml
- ^(.*/)?app\.yml
- ^(.*/)?index\.yaml
- ^(.*/)?index\.yml
- ^(.*/)?#.*#
- ^(.*/)?.*~
- ^(.*/)?.*\.py[co]
- ^(.*/)?.*/RCS/.*
- ^(.*/)?\..*
- ^(.*/)?.*\.bak$
- ^django
- ^non_gae_indicator
Now we have a way to tell whether we are running under the GAE-sdk or live - since non_gae_indicator won't be available when we are live.
So in main.py you can do:
if not os.path.exists(os.path.abspath(os.path.dirname(__file__)) + '/non_gae_indicator'):
# GAE
from google.appengine.dist import use_library
use_library('django', '1.1')
else:
# Not GAE - Add our django package to the path
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)) + '/django')
You should run your local SDK server with the --allow_skipped_files flag (or else the skipped files will appear to not be exist when checking them - the server console gives a warning about it).
#stallarida - The problem is that .96 is shipped as default with the SDK. What I did in the end, which is a dirty hack but works, is to update the version of django in the appengine directory to 1.1. Worked fine, needed a bit of tweaking between dev and production.
Specifically I had to comment out use_library('django', '1.1') when running locally but include it when uploading my app.
I'm sure there's a better solution and I'll work it out when my linux experience improves.