django

Customized Django template filters

This is a lazy post and more of a bookmark, but I found a really straightforward and clear tutorial for creating your own django template tags.

http://www.pfinn.net/custom-django-filter-tutorial.html

 

Django’s own documentation is unfortunately not very beginner friendly!

Advertisements

static! static everywhere!

So having turned my Django debug to false, I realise it no longer serves static files for me and my webserver is not doing its part in holding up its end of the bargain!

Talk about dropping the ball!

drop-ball-o

 

I’m still figuring out how to set up my webserver to handle static files. Is it a matter of changing the links in my settings.py and urls.py to hardcode them to my website? Or do I have to get my hosting to set up Apache’s httpd.conf to serve static files (which I do not have access to)

Deploying Django site

Nope, I’m not done yet, but I’m keen to have at least half my site live so that I can stop looking shame-faced and shifty-eyed when people ask about my website. Especially if just a moment before I was running an elevator pitch.

Following that with “uh, my website is under construction” is the equivalent to Billy and Donald’s great efforts at the wave here:

Donald-Trump-Bill-O-Reilly-Wave-Yankee-Stadium

 

So with all the security scare about Heartbleed bugs and keeping secret keys, while, secret, I still have no clue what to do different when deploying as vs. to developing. You mean I don’t just upload the whole lump onto a live server and point my domain name at it?

Tommy Jarnac has a brilliant post on the best practices for moving a Django project into production mode.

Meanwhile, I’m taking so long on my website I wonder if I should just turn it into a ‘reality’ tv show and let visitors see my bits and pieces of experimentation LIVE.

 

Extending a base.html

Further digging into the templates of my website, I’ve tried setting up a base.html and using it to extend throughout the rest of my website. Seem to have a problem getting Django to find it though, because right now it says

base.html template does not exist

although it’s parked right next to all my other htmls in my templates/wagweb folder. hmm… reminds me of when static files were not loaded. Something is differently pointing wrongly!

{% static %} files

Now i’ve finally moved on to creating my own templates within django/wagtail. But first I have a bit of an issue with the static files.

When I attempt to add images in Wagtail,

Picture 12

I get the dreaded Internal Server Error

Internal Server Error
The server encountered an internal error or 
misconfiguration and was unable to complete 
your request.

 

 

 

 

 

 

 

 

Picture 11

 

Additionally, attempting to link to images in my static folder like so:

<p><img src="{% static "its_alive1.gif" %}" alt="Its Alive" /></p>

gives a broken image, although if I navigate to http://username.com/static/image.gif I can see the image in my browser.

Right now my settings.py are as follow:

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/home/clarayee/website/static/'
STATICFILES_DIRS = ()

The STATIC_URL is a hard-rooted to test if it makes any difference. Nope, no difference!

Migrations

flock of migrating canada geese birds flying at sunset

Yes, migration! I would like some too! Who doesn’t love to travel?

PK Shiu has an amazing explanation of how South’s migration works here.

looking at my wagweb migrations, I realise:

from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
 # Adding model 'BlogPage'
 db.create_table(u'wagweb_blogpage', (
 (u'page_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['wagtailcore.Page'], unique=True, primary_key=True)),
 ('body', self.gf('wagtail.wagtailcore.fields.RichTextField')()),
 ('date', self.gf('django.db.models.fields.DateField')()),
 ))
 db.send_create_signal(u'wagweb', ['BlogPage'])

 

There’s BlogPage. Probably why BlogPage is working fine. But where’s my BlogIndexPage?

okay, time to make them migrate that:

[~/website/wagtailweb]# ./manage.py schemamigration wagweb --auto
 + Added model wagweb.BlogIndexPageRelatedLink
 + Added model wagweb.BlogIndexPage
Created 0002_auto__add_blogindexpagerelatedlink__add_blogindexpage.py. You can now apply this migration with: ./manage.py migrate wagweb

Boom!

Following which I will follow instructions and migrate again:

Running migrations for wagweb:
 - Migrating forwards to 0002_auto__add_blogindexpagerelatedlink__add_blogindexpage.
 > wagweb:0002_auto__add_blogindexpagerelatedlink__add_blogindexpage
 - Loading initial data for wagweb.
Installed 0 object(s) from 0 fixture(s)

Andd………?

 

IT WORKS!

bimo

I swear BMO is shouting DJANGO in her heart.

 

 

 

Table Errors

I am no longer getting the Template errors when I click ‘View Live’ although I haven’t done anything in the pass few days. Sometimes I swear my computer does stuff on its own while I’m away.

its_alive1

Right now, Blog Page works, Root Page works. I can publish, save drafts and preview for both.

I’m only getting errors with Blog Index Page.

When I attempt to Publish Blog Index Page I get error

Picture 10

Exception Type: OperationalError at /admin/pages/new/wagweb/blogindexpage/2/
Exception Value: no such table: wagweb_blogindexpage

 

Definitely something to do with my SQlite database right? It’s probably a wrong path somewhere which is causing my SQlite to not find the table.

If i run manage.py migrate from shell,

 raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError treebeard: No module named treebeard

Oops! I think I saw this before. So that wasn’t resolve then.

I’m going to very simple try a pip install django-treebeard

running manage.py migrate this time gives me

 File "/home/clarayee/env/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 450, in execute
 return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: table "wagweb_blogpage" already exists

wagweb_blogpage is working fine. I’m not sure why it’s still trying to create a new table for that?

But trying to save draft in my dashboard still returns error no such table.

I’m going to ./manage.py migrate wagweb again to update the database and see if it changes anything.

Running migrations for wagweb:
- Nothing to migrate.
 - Loading initial data for wagweb.
Installed 0 object(s) from 0 fixture(s)

Nothing updated. Error still there. Am reading up on migrations now.

 

 

 

Pronouncing DJANGO

So in the process of complaining to friends about my stupidities, I had accidentally pronounced Django [Di-jang-go]

Yes, with the ‘D’ in capitals.

tumblr_mgjndwFQkS1rxbipho5_250

 

Of course I had to say that to a table of people who knew Spanish, and smoothly proceeded to pretend it was all a huge joke and like duh, i know how it’s read, like DJango [D-J-ango]. 

tumblr_mkhy8t93Mk1s7k6hpo1_500

Note to self: always drop the ‘D’

Thank goodness Python and Wagtail are unmistakable. Wait, they are in English right?