Just notes on what I’m up to in trying to get django to work for my Computer Science 462 class. I find this helpful for remembering where I left off and commands etc. But you will probably find it very boring unless you try and do the same.
using tutorial at http://docs.djangoproject.com/en/dev/intro/tutorial01/
==========================
Found I needed root priviledges, so I allowed the default users, ‘ubuntu’ root priledges by calling
so I logged into bash as root by just typing
|sudo bash
then editing the /etc/sudoers file by typing
|visudo
and adding changing the line from
root ALL=(ALL) ALL
to
ubuntu,root ALL=(ALL) ALL
===========================
I edited the settings file settings.py, and configured stuff for sqlite3, like the tutorial suggested.
then, as it suggested, ran
python manage.py syncdb
which created the database file where I wanted it in /var/django/db/mikeServ.db
tried to view the schemas by typing
|.schema
with no luck. googled how to view schemas in sqlite.
Googled what the matter might have been, found I needed pysqlite2, so ran
|aptitude search pysqlite2
found it, and ran
|apt-get install python-pysqlite2
but still can’t find anywhere how to use it really, nor what comes with that package
bah. I’ll just assume it worked for now, even thought I can’t check it. we shall see.
I want to use django for a class project which really doesn’t require a local database, just using ajax and templates, so I skipped over the rest of the first page of teh tutorial having to do with creating objects/tables, but it seeemd good.
was interested in the admin pages to work, so I followed the instructions on teh next page:
added django.contrib.admin to INSTALLED_APPS in settings.py,
and ran
|python manage.py syncdb
then edited urls.py as indicated.
Tried to run the /admin/ pages, but no go. I think I might need to restart the webserver:
|/etc/init.d/apache2 restart
When I visited my site (http://50.16.162.246/admin/) the next time, made some progress: got an error message! somethign about indentatino error in ursl.py, so I’ll go fix taht. (Found the compy was right: forgot to remove preceding white-space when uncommenting).
Now though, I’m getting a 500 error with no real helpful input. Hmm. progress? probably. I bet the error is coming from not reaaally having the database schemas, though I ran syncdb ‘n all. I should verify those exist for reals.
I foudn the problem was I don’t have django stuff on my path, as explained in http://martinjansen.com/2008/10/20/django-settings-files-for-development-and-production/. I just need to add my settings.py file to the PATH, and some DJANGO_SETTINGS.
Trouble with that, some help also on http://ubuntuforums.org/showthread.php?t=125735
Found that didn’t permanently solve anything. (This forum page has a good explanation of different config files like .bashrc and .bashprofile and stuff: http://ubuntuforums.org/showthread.php?t=125735).
Eventually, found an easy way to add to the PYTHONPATH is just to add a file named whatever.pth to something that already is on a the pythonpath (like /usr/local/lib/python2.6/dist-packages) and in that file write nothing but the path to whatever I want to add to the python path.
Still the DJANGO_SETTINGS_MODULE isn’t found.
Then I realized I had edited the wrong .bashrc file. I edited /.bashrc, when really I should ahve edited ~/.bashrc. bah!
So edited taht file instead, restarted teh command-prompt shell, and voila!
I could verify that PYTHONPATH was set by typing in
|$PYTHONPATH
into the shell and it would print it out
However, I’m still getting a 500 error on teh admin pages.
Added a symbolic link to the admin media stuff like suggested on http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/, but that didn’t resolve it.
Now I’m looking at http://code.djangoproject.com/wiki/django_apache_and_mod_wsgi
Ok, more of the same. But I’m onto something now.
I thought I really needed to see some better error logs. So I searched for where are the apache error logs, and found it’s at:
/var/log/apache2/error.log, right next to the access.log (I didn’t know they were seperate). I then noticed what the error seems to be:
[Sat Jan 29 16:25:55 2011] [error] [client 67.166.99.183] OperationalError: attempt to write a readonly database
meaning the sqlite file that its trying to write to is set to be read-only. Which kinda makes sense. So I’m gonna check what the files permissions are, and change them to amke them write-whatever-you-want. (Did that by going to the file, which I placed in /var/django/db/mikeServ.db and did the command chmod 777 mikeServ.db)
and BOOM BABY! Admin console!
Except I didn’t create the symbolic link to the media files right.
And then I noticed it stillw asn’t redirecting quite right. So I needed to edit the apache config file some more. Basically, I needed to make apache get the media files from where they’re at on the server. So I added these lines to my /etc/apache2/sites-available/default file:
Alias /media/ /usr/lib/pymodules/python2.6/django/contrib/admin/media/
Order deny,allow
Options Indexes
Options FollowSymLinks
Allow from all
==================================
ok so now I’m trying to get some pages to work,
I ran
|python manage.py startapp view
|” ” ” list
|” ” ” home
to create 3 ‘apps’ I guess that will host my files
and added those 3 things to my list of installed apps
and another issue is I’m not sure if django-admin.py is on my filepath. …actually immediately remembered: it is, just under a different name, it’s under django-admin (no ‘.py’)
so I finished those views.py files, as follows:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Create your views here.
from django.http import HttpResponse
from django.template import Context, loader, Template
import urllib2
import json
def popular(req):
t = loader.get_template(‘popular.tmpl’)
return HttpResponse(t.render({}))
def recent(req):
t = loader.get_template(‘recent.tmpl’)
response=urllib2.urlopen(‘http://imaj-app.lddi.org:8010/list/recent’)
dictResponse=json.loads(response.read())
c = Context({‘imagelist’: dictResponse[‘images’],’nextsubmitdate’:dictResponse[‘nextsubmitdate’],’nextratesort’:dictResponse[‘nextratesort’]})
return HttpResponse(t.render(c))
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
and then the template:
<<<<<<<<<<<<<<<<<<<<<<<,
.listitem {
margin-bottom: 2em;
}
.listitem a {
float:left;
margin-right: 2em;
}
Recent Images
{% for image in imagelist %}
{{image.description}}
Submitted by {{image.submituser}} on {{image.submitdate}}
{% endfor %}
{% if nextsubmitdate %}
{% endif %}
>>>>>>>>>>>>>>>>>>>>
the ta gave the template to us originally in some ‘cheetah’ python template language, but converting it to django’s template language was easy: the semantics (the way it works) are just the same, just silly difference in syntax.
and voila! I have a page which renders! bwahahaha!
I noticed the page was missing headers, css stuff, etc.
After inspecting the templates, I realized they had no declaration of etc, or importing of css files. Reason: I wasn’t using the main template at all!
Apparently Cheetah works differently from django’s template. In Cheetah the parent template imports the others by just inserting $main somewhere in teh html. IN django, you need a tag in the parent saying
{% block WHATEVER %}{%endblock%} and in the child template, you need to start with {
%extends ‘WHATEVERYOUCALLEDTHEPARENT.tmpl’%}
and then
{%block WHATEVER%} {%endblock%} (note that the names must match)
And then when it loads the child template, it’ll look for teh parent, load it, and insert the child template where indicated.
About that css stuff. Sure enough, it wasn’t finding the css.
I read somewhere about how you can serve static files simply using apache. That makes sense, but I think somehow my django config was interfering with the usual serving of static files from my directory of /var/www.
So, I poked around and noticed that I could just add
(r’^site_media/(?P.*)$’, ‘django.views.static.serve’,{‘document_root’: ‘./site_media’,’show_indexes’: True}),
to my list of urlpatterns in urls.py. But it wasn’t working.
After a bit, i noticed the reason: ‘./site_media’ was meant to point to where the static files folder was located on the hard drive. I suppose it’s meant to be a relative uri, but it wasn’t working. Switched it to a absolute uri and it worked.
Starting making another page which was basically identical to my first one
(which just sends out a request and then parses it into a json object and displays it) and got a goofy error saying something about how the dict module had no function named ‘push’. Really random. I saw it before htough. And I had the advantage of this page being basically the exact same as the previous one. So I compared them: identical. Then I remembered: sometimes you need to restart apache for the python to get reloaded. So restarted apache and it worked.
wanted to be able to log stuff on my server.
SO, I followed the instructions on http://djangosnippets.org/snippets/1731/,
and added ‘site_logging’ to my list of installed apps, and added the site_logging.py file in the root of my django/mikeServ directory. Then called logging as instructed (even though I would have thought I should have been calling ‘site_logging’, but just ‘logging’ did it.)
Viewed the error log using
|tail -f /var/log/apache2/error.log
BOOM BABAY!