Django on 34sp.com
Goto page 1, 2  Next
 
Post new topic   Reply to topic    34SP.com Forum Index // Scripting Support
View previous topic :: View next topic  
Author Message
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Wed Nov 12, 2008 12:16 pm    Post subject: Django on 34sp.com Reply with quote
At present only the following servers are setup with this functionality but other plesk 8.2+ servers can be setup on request

adam.34sp.com
kitt.34sp.com
ted.34sp.com
ash.34sp.com

This article below is provided by Stephen Watkin who helped with the initial testing of this setup.

Django on 34sp.com

If you've picked up Django from any of the tutorials (either the Django getting started documentation or the djangobook.com book) you'll be familiar with the concepts of projects and apps. (A project is typically a collection of one or more apps, usually all running from a single database). In this document we'll look at how to get a single project served up via Apache on the 34sp.com servers. We'll assume you've got the project up and running locally using the django development server.

What is a project?

The 'bare bones' of a Django project consist of just a handful of files. Although the django-admin utility is usually used to create the basic project files there's nothing really special about them - you can just as easily set them up yourself by hand. All you need is a folder containing 4 files:

    * __init__.py - an empty file that tells python to treat this folder and it's contents as a python module

    * .htaccess - containing the instructions telling Apache to handle the files with mod_python

    * settings.py - containing project settings, which should only need slight adjustments from the file used by the development server

    * urls.py - the base urlconf, as referenced by the settings file, which is usually used to hand of requests to different apps in the project.


In addition to these files you'll obviously need your app(s) containing your views and templates. Apps are usually contained within their own subdirectory, containing their own urlconf files. With this in mind there's really no reason why you can't have as many Django projects set up as you like!

If you're using sqlite, be aware that the project folder and the database itself must be chmodded 777 to be readable and writeable by the web server.


Example set-up

Let's say we have an account called 'example.netmx.co.uk' and we want to set an application up called 'blog'. We should just be able to copy our project straight over from the development environment to the httpdocs folder with only the following changes. Obviously you'll want to replace all instances of 'example.netmx.co.uk' and 'blog' with whatever applies to you.

httpdocs/blog/.htaccess
Code:

SetHandler python-program
AddHandler mod_python .py
Header set Pragma "no-cache"
Header set Cache-Control "no-cache"
Header set Expires "-1"
PythonPath "['/usr/local/psa/home/vhosts/example.netmx.co.uk/httpdocs/blog'] + sys.path"
PythonOption django.root /blog
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE blog.settings
PythonDebug On


This .htaccess file behaves like any other, basically telling Apache to treat whatever is in this directory differently than normal - in this case, handing control over to mod_python which in turn hands the request over to Django.

    * The PythonPath line simply adds the 'blog' folder to the list of directories python looks in when you use the import statement, so anywhere in your code you can say import blog.<appname>.models and Python will know to look in the blog/<appname>/ directory for that module.

    * The PythonOption django.root /blog line performs a bit of wizardry with the URL before passing it on to the urlconf file, basically stripping (in this case) the /blog part of the URL so you don't have to go through every line in your urls.py files adding '/blog' to the beginning of each regular expression. Note the lack of a trailing slash.

    * The SetEnv DJANGO_SETTINGS_MODULE blog.settings line effectively loads your settings file into Django, telling it where to find your database and urlconf files.

    * The PythonDebug On line tells Django to throw one of it's nice informative error screens each time it encounters an error. If this is set to 'off', the user simply sees the standard '500 Internal Server Error' message, which is recommended for production use.


httpdocs/blog/settings.py
The settings file shouldn't need much alteration when moving from the development server to apache. The main things to look out for are:

    * The MEDIA_URL setting should be set to an absolute URI that resolves to your 'static media' directory - ie., where you keep all your images / css / javascript referenced by your templates. This only really comes in to play if you use the {{ MEDIA_URL }} template tag - you're perfectly free to hard-code everything and not have to bother with this at all, although it's well worth at least considering the feature. In my case I like to keep all the static media in a sub-directory the Django project directory (httpdocs/blog/media/), which makes mine MEDIA_URL = 'http://example.netmx.co.uk/blog/media/'

    * If you use SQLITE you need to specify the full, absolute pathname to your database file in the DATABASE_NAME setting. In our case it might look something like DATABASE_NAME = '/usr/local/psa/home/vhosts/example.netmx.co.uk/httpdocs/blog/blog.db'

    * If you use the built-in admin application, you're probably best to set your ADMIN_MEDIA_PREFIX to '/djangomedia/' like so: ADMIN_MEDIA_PREFIX = '/djangomedia/' - /djangomedia' is a special symlink set up by 34sp.com that resolves to the folder containing all the static media for the admin app.

    * LOGIN_REDIRECT_URL, LOGIN_URL and LOGOUT_URL - if you're using the auth application with this method (putting the django project in a sub-folder) you may need to pay attention to these settings, depending on how / if you're using any mod_rewrite magic on your root httpdocs folder. To cut a long story short, when the auth system redirects you to a login page it doesn't take into account the special PythonOption django.root /blog we specified earlier - by default it'd just send you straight to http://example.netmx.co.uk/accounts/login/, which probably doesn't exist - really it should redirect the user to http://example.netmx.co.uk/blog/accounts/login/ - depending on your setup, you'll need to adjust these as appropriate.


Redirecting users to the site
Right now our site lives in a subdirectory called 'blog/' although at some point we're probably going to want it to appear at the root of the domain (i.e., http://example.netmx.co.uk/ instead of http://example.netmx.co.uk/blog/). I find the easiest way to do this is to set up an .htaccess file in the httpdocs/ folder and use some mod_rewrite, like so:

httpdocs/.htaccess
Code:

RewriteEngine On
RewriteRule ^(.*)$ blog/$1 [L]


This intercepts all requests made to the domain and sends them to the 'blog/' subdirectory without the user knowing, complete with any further URI elements such as the query string.

And that should be it! The only other advice I'd give is try and keep your apps decoupled from your project, which something that the official documentation only lightly touches on, but is worth trying to get right from the start in order to save some potential headaches down the road. Google has plenty of information on the best ways to do this.
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Tue Nov 18, 2008 2:44 pm    Post subject: Reply with quote
Django support has been added to the ash.34sp.com server as well now.
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
OniDaito
34SP Newbie
34SP Newbie


Joined: 23 Nov 2007
Posts: 4

PostPosted: Wed Nov 26, 2008 10:05 am    Post subject: Reply with quote
Thats brilliant! I shall be coding right away!

Cheers
Ben
Back to top
View user's profile Send private message
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Fri Dec 26, 2008 5:07 pm    Post subject: Reply with quote
There seems to be a few interesting problems with this setup.

Firstly, can you guys confirm if the MySQL module is working or not?

More importantly, it appears the URL resolvers are not working too well. If I attempt to access a directory that actually exists on my webspace, no problem, its handled. however, a virtual directory isnt handled by django and I get a 404. An example would be:

http://www.section9.co.uk/SnowdropStudios/paintings

this works because the 'paintings' directory exists.

However:

http://www.section9.co.uk/SnowdropStudios/paintings/1

does not work.Django should handle the /1 and deduce that im after painting with ID 1. However I get a 404.

Also, the ADMIN section doesnt work either. The only thing that seems to work are these URLs that map to dirs that actually exist.

Here is my .htaccess
Code:

SetHandler python-program
AddHandler django.core.handlers.modpython .py
Header set Pragma "no-cache"
Header set Cache-Control "no-cache"
Header set Expires "-1"
PythonPath "['/usr/local/psa/home/vhosts/section9.co.uk/httpdocs/'] + sys.path"
PythonOption django.root /SnowdropStudios
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE SnowdropStudios.settings
PythonDebug On
Back to top
View user's profile Send private message Visit poster's website
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Sat Jan 03, 2009 2:35 pm    Post subject: Reply with quote
..bump..
Anything blindingly obvious i've missed here? Something to do with virtual urls or what not i'd imagine...
Back to top
View user's profile Send private message Visit poster's website
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Sat Jan 03, 2009 9:12 pm    Post subject: Reply with quote
In regards to the urls have you setup the urls.py file accordingly so it knows how it should map these?

What issue are you having when trying to use mysql and the auto built admin items?
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Sun Jan 04, 2009 12:13 pm    Post subject: Reply with quote
This is the main URL conf. I should point out the admin interface does not work.

Code:
from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^SnowdropStudios/', include('SnowdropStudios.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^paintings/', include('SnowdropStudios.paintings.urls')),
    (r'^cart/', include('SnowdropStudios.shop.urls')),
    (r'^$', 'SnowdropStudios.paintings.views.categories'),

    # Uncomment the next line to enable the admin:
    (r'^admin/(.*)', admin.site.root),


)


And the main file in question...

Code:

from django.conf.urls.defaults import *


urlpatterns = patterns('SnowdropStudios.paintings.views',
    (r'^$', 'latestFive'),
    (r'^(?P<painting_id>\d+)/$', 'detail'),
    (r'^categories/(?P<category_id>\d+)/$', 'categoryList'),
    (r'^categories/','categories')
)


Again, on the DEV server it all works fine so I suspect the URL configs are correct.

One thing I would bring to your attention is this line:


Code:

PythonPath "['/usr/local/psa/home/vhosts/section9.co.uk/httpdocs/'] + sys.path"
PythonOption django.root /SnowdropStudios


This is from the htaccess file and it DIFFERS from yours because the PythonPAth is set as the one that contaisn the dir with my project in it. Anything else doesnt work and its different from your instructions.
Back to top
View user's profile Send private message Visit poster's website
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Sun Jan 04, 2009 12:47 pm    Post subject: Reply with quote
Please try setting your PythonPath as

PythonPath "['/usr/local/psa/home/vhosts/section9.co.uk/httpdocs/','/usr/local/psa/home/vhosts/section9.co.uk/httpdocs/SnowdropStudios'] + sys.path"

Also do you have PythonDebug still enabled and set up in your settings.py?

Im afraid im really not a django programmer but if the application is producing error codes we maybe able to aid, have you looked into the error_logs and such.
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Sun Jan 04, 2009 1:20 pm    Post subject: Reply with quote
Code:

# Django settings for SnowdropStudios project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG


Still set.

set the .htaccess to what you mentioned. No joy. I still get 404s when I enter the old URLs. I HIGHLY suspect this is an apache config issue. I reckon it must not be passing such URLs to the python interpretor to deal with.

the MySQL is a seperate issue but the SQLite is working fine for now which is great. Im not sure where i should be looking for error messages. Im still not too used to error messages with apache and python. Normally, if its a Django error, it will get printed to the screen in typical Python format. With this however, i think its a server thing.
Back to top
View user's profile Send private message Visit poster's website
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Sun Jan 04, 2009 1:54 pm    Post subject: Reply with quote
I have just taken a quick look at your site.

You have a .htaccess in httpdocs for using SEO friendly urls for wordpress, this is converting the 'no real page' into a wordpress page.

You will need to remove this .htaccess to let the python url handlers come into play.

You maybe able to wrap the mod_rewrite lines in something like

<Filesmatch "!^SnowdropStudios">
<IfModule mod_rewrite.c>
....
....
</IfModule>
</FilesMatch>

So it just ignores doing the mod_rewrite for your django project.
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Sun Jan 04, 2009 5:07 pm    Post subject: Reply with quote
Dude you are a smarter man than I Seems to have worked rather well that.
Cheers!
Back to top
View user's profile Send private message Visit poster's website
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Mon Jan 05, 2009 8:43 am    Post subject: Reply with quote
Excellent glad that worked.

What was the issue you had with mysql module - if you do try this again and have issues still let me know and ill check this out further
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Mon Jan 05, 2009 10:40 pm    Post subject: Reply with quote
I shall look at the database shortly. There are some troubles here and there

Still, I was premature with the .htaccess thing. The filematch directive seems to turn everything off fullstop

<Filesmatch "!^SnowdropStudios">

This doesnt match anything sadly so the wordpress is now broken
Back to top
View user's profile Send private message Visit poster's website
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Tue Jan 06, 2009 7:49 am    Post subject: Reply with quote
Ok get rid of the FilesMatch -- and try adding before the first RewriteCond

RewriteCond %{REQUEST_URI} !^/SnowdropStudios/

But the main issue is that you have two things trying to rewrite urls that dont really exist.
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Wed Jan 07, 2009 9:07 pm    Post subject: Reply with quote
Thats right, it worked. I was just suprised the ^!SnowdropStudios bit didnt match was all. The admin site doesnt seem to want to access the database:

Code:

MOD_PYTHON ERROR

ProcessId:      46155
Interpreter:    'section9.co.uk'

ServerName:     'section9.co.uk'
DocumentRoot:   '/usr/local/psa/home/vhosts/section9.co.uk/httpdocs'

URI:            '/SnowdropStudios/admin/'
Location:       None
Directory:      '/usr/local/psa/home/vhosts/section9.co.uk/httpdocs/SnowdropStudios/'
Filename:       '/usr/local/psa/home/vhosts/section9.co.uk/httpdocs/SnowdropStudios/admin'
PathInfo:       '/'

Phase:          'PythonHandler'
Handler:        'django.core.handlers.modpython'

Traceback (most recent call last):

  File "/usr/local/lib/python2.5/site-packages/mod_python/importer.py", line 1537, in HandlerDispatch
    default=default_handler, arg=req, silent=hlist.silent)

  File "/usr/local/lib/python2.5/site-packages/mod_python/importer.py", line 1229, in _process_target
    result = _execute_target(config, req, object, arg)

  File "/usr/local/lib/python2.5/site-packages/mod_python/importer.py", line 1128, in _execute_target
    result = object(arg)

  File "/usr/local/lib/python2.5/site-packages/django/core/handlers/modpython.py", line 228, in handler
    return ModPythonHandler()(req)

  File "/usr/local/lib/python2.5/site-packages/django/core/handlers/modpython.py", line 205, in __call__
    response = middleware_method(request, response)

  File "/usr/local/lib/python2.5/site-packages/django/contrib/sessions/middleware.py", line 35, in process_response
    request.session.save()

  File "/usr/local/lib/python2.5/site-packages/django/contrib/sessions/backends/db.py", line 58, in save
    obj.save(force_insert=must_create)

  File "/usr/local/lib/python2.5/site-packages/django/db/models/base.py", line 307, in save
    self.save_base(force_insert=force_insert, force_update=force_update)

  File "/usr/local/lib/python2.5/site-packages/django/db/models/base.py", line 379, in save_base
    result = manager._insert(values, return_id=update_pk)

  File "/usr/local/lib/python2.5/site-packages/django/db/models/manager.py", line 138, in _insert
    return insert_query(self.model, values, **kwargs)

  File "/usr/local/lib/python2.5/site-packages/django/db/models/query.py", line 894, in insert_query
    return query.execute_sql(return_id)

  File "/usr/local/lib/python2.5/site-packages/django/db/models/sql/subqueries.py", line 309, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)

  File "/usr/local/lib/python2.5/site-packages/django/db/models/sql/query.py", line 1734, in execute_sql
    cursor.execute(sql, params)

  File "/usr/local/lib/python2.5/site-packages/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)

  File "/usr/local/lib/python2.5/site-packages/django/db/backends/sqlite3/base.py", line 168, in execute
    return Database.Cursor.execute(self, query, params)

OperationalError: unable to open database file



Going to keep looking to make sure I havent missed anything obvious. Reads normally for the rest of the time (i.e normal browsing)
Back to top
View user's profile Send private message Visit poster's website
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Wed Jan 07, 2009 9:13 pm    Post subject: Reply with quote
As this is an sqlite database make sure that the directory it resides in is fully writeable and that the sqlite database itself is as well (chmod 777)
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Wed Jan 07, 2009 9:59 pm    Post subject: Reply with quote
Ah, I had 777 on the DBFile but not on the dir. Sorted now. Cheers. Havent tried MySQL in anger yet but might give that a go at a later point.
Back to top
View user's profile Send private message Visit poster's website
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Wed Jan 21, 2009 6:36 pm    Post subject: Reply with quote
Actually I have a another slight problem. The Python Imaging Library is a requirement for Django but it appears I cannot import it. Im getting an Import Error for PIL

from PIL import Image

Apparently PIL cannot be found
Back to top
View user's profile Send private message Visit poster's website
imknight
Administrator
Administrator


Joined: 16 Mar 2001
Posts: 3627
Location: Stroud,Gloucestershire

PostPosted: Thu Jan 22, 2009 4:08 pm    Post subject: Reply with quote
Should now be available on this server for you.(ted.34sp.com)
_________________
Ian
34SP.com
Easy script installers for a small number of popular scripts are now available at http://scripts.34sp.com
Used our script installers? Please post your site here

| Wordpress Hosting |
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Oni
34SP Newbie
34SP Newbie


Joined: 30 May 2001
Posts: 24

PostPosted: Wed Apr 29, 2009 11:03 am    Post subject: Reply with quote
Seems like I'm getting another annoying Error, this time with SQLLite

Code:
TemplateSyntaxError at /paintings/

Caught an exception while rendering: database disk image is malformed


This is a new one. 777 chmod and all the rest is done. Is there a different version of SQLite going down here? I suspect thats what it is. What version do we have atm? I cant quite tell.

Cheers
Back to top
View user's profile Send private message Visit poster's website
Post new topic   Reply to topic    34SP.com Forum Index // Scripting Support All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Powered by phpBB © 2001, 2002 phpBB Group