Getting an existing app to run with django 1.4

Hello everyone,

I made a web poll with django 1.4 and sqlite3 and it worked as long as i tested it on localhost. Now that I want to host it on PA I come across so many bugs and errors. Although I managed to solve lots of them the app is still not working properly and I dont know what to do anymore.

For example: I can't reach my index.html anymore. I can reach but whenever I try to reach index, It wont work. Then the voting tries to reach the wrong page instead of going to the next question. Also the results which were supposed to be written into a text file arent saved properly anymore. I dont understand what happened, as it worked perfectly fine on my computer.

As you can see I am near desperate already, so if you had some hints for me, I would be very grateful.


Hi ulli91,

Okay, let's work on these problems one by one. Starting with how you installed the previously written application.

The system version of Django on PythonAnywhere is 1.3. You need to create a virtualenv with 1.4 and put your application inside that. Have a look at the PythonAnywhere virtualenv tutorial

index.html? Are you trying to serve a static html file? You'll need to setup static files somehow. You could read the PythonAnywhere guide to serving Django static files. However I wouldn't recomend serving index.html as a static file. Use a django view to serve the templated file called index.html instead.

Not being able to write to a text file sounds like you have a path problem. Are you using an absolute path like /home/ulli91/project/textfile.txt or are you just using textfile.txt? That might not work because it could be trying to write it into a directory that you don't have write permissions for.

Anyway, that's a start. If you tell us where you are getting stuck I can keep helping.


@ulli91: Welcome to PA. You'll find it's a great community!!

Hello Hansel,

I already installed a virtualenv and I hope I am using it correctly. I also have a setup for all static files (images). The html files are templates for django. It is not that it doesnt write at all. It just writes wrong stuff. Before it looked like that: Spaß: Ja, (Spaß = Fun is the poll question and Ja = yes is the choice/answer) Individualismus:Ja, Logik: Nein, etc.

Now it just writes "Choice object, ".

I am using f = open("/home/ulli91/static/hauptmarken.txt", "r") for the text file.

Thank you so much for your help!

Hi ulli91,

That sounds like you are just writing str(my_choices_object) to the file. It's possible that you are using different versions of Django on PythonAnywhere and your local machine. In the version of Django on your local machine the models have a different default __str__ method.

Do you think that your virtualenv is actually working? To test this you could add the following to your file where you activate it.

import django
raise Exception(django.VERSION)

That will print the django version into your error logs.

It says:

2013-03-11 15:52:56,974 :Traceback (most recent call last): 2013-03-11 15:52:56,975 :Exception: (1, 3, 5, 'final', 0)

Right, okay so the (1, 3, 5, 'final', 0) is the Django version number. You are running under Django 1.3.5. Which would explain all the errors (or differences) that you've been experiencing. You should go back and have a look at the virtualenv instructions and make sure you followed them through properly. I'll run through them myself and make sure I can get them to work.

The problem is that my partner and I installed the virtualenv several times and we followed the instructions as if our lives depended on it. Even if I activate virtualenv in the bash console (and it says django14: all the time) the error message says that its just 1.3.5.

Same here, 1.3.5 instead of 1.4 even if it says 14 in the console!

There are a couple of possibilities. To separate them, do the following in a bash console with the virtualenv activated:

pip freeze | grep Django

check the version of Django that pip thinks is installed in the virtualenv

pip install -U django==1.4.5

see whether a specific version installs cleanly

pip freeze | grep Django

check that the upgrade worked

python -c "import django; print django.version"

Then do the trick that @hansel suggested for checking the Django version in the web app.

pip freeze says:

Django==1.4 wsgiref==0.1.2

if I type grep Django nothing happens except that I can type but not use commands anymore. how do I change that?

The grep was supposed to be on the same line after a | symbol to just reduce the noise. It looks like you have Django 1.4 correctly installed in the virtual env. Please post your wsgi file. I suspect that you're not activating the virtualenv in you web app.

Sorry I can't write a "|" in the console, so I thought it was supposed to be another line.

activate_this = '/home/ulli91/.virtualenvs/django14/bin/'
execfile(activate_this, dict(__file__=activate_this))

import os
import sys

path = '/home/ulli91/mysite'
if path not in sys.path:
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

[Edited by admin to format code correctly]

Now I'm puzzled. That looks correct. Do you mind if I poke around in your web app a bit?

No I dont mind at all. Anything if it might help.

@ulli91, Your web app is correctly set up and running Django 1.4. The only way I can think that you would have seen 1.3.4 as the Django version would be if you put the import django; raise Exception(django.VERSION) code into your uwsgi file before the virtualenv activation.

As for not being able to see the index, you have not defined an index (i.e. /) in your Is the index on your local machine a static file? Or maybe it's being sneakily served by something else that's only present on your local machine?

The index is of no interest for the database or program. The important part is .../polls/4/ where the poll is created out of a template and the id (here 4)

Could you tell us a specific example of a page that's generating an error? That would definitely help working out what the problem is. For example, if I go to I get a page that looks correctly rendered; is there something I can do there that will show the problem?