Monday, 25 August 2008

2. Your First Django Site: A Simple CMS

This is part 2 of a series of posts on James Bennett's excellent Practical Django Projects. The table of contents and explanation can be found here.

Configuring Your First Django Project

Choosing an editor for python is a personal choice. As a beginner you can do worse than just use IDLE which comes with most python distributions (separately on os x)  or pythonwin if you are on the windows platform. These have the advantage of autocompletion of function arguments, but once you're comfortable you're likely to move onto something that manages a project nicely and is more developed. I use Textmate on OS X but there's a full list here. If I have been working on nonpython projects I miss not having autocomplete in Textmate but sometimes I'll just keep IDLE open just for that.

Putting Together the CMS

On p13 is where the first deviation from the older django kicks in. In your settings.py add 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware', (don't forget the comma) to your MIDDLEWARE_CLASSES. Django 1.x includes a new forms admin which separates out the administrative configuration from the models. By default the new urls.py includes:
 # Uncomment the next two lines to enable the admin:
 # from django.contrib import admin
 # admin.autodiscover()  
 # Uncomment the next line for to enable the admin:
 #   (r'^admin/(.*)', admin.site.root),
Which you will want to uncomment to enable the admin
Instead of
 
# (r'^admin/', include('django.contrib.admin.urls')),
On page 15 you must make sure you edit the example.com site instead of adding a new site. You might have noticed in your settings.py the SITE_ID = 1. If you add a new site 127.0.0.1:8000 then that will have a SITE_ID of 2, and in the following section flatpage views filter by default on the current site which is 1.

The urls.py is where beginners face one of their biggest hurdles. On the face of it regular expressions are difficult to craft without practice, and implementations subtly vary from language to language. Time to add to the bookshelf with another bible, Mastering Regular Expressions which is probably more than you'll ever need. On the free front A.M. Kuchling has a great python how-to. One aspect of regexes that irritates is trying to find tools that you can use to test them easily while you are building your own. A few of the tools I have found for OS X have been inconsistent only modestly useful. One online recommendation is this site though I haven't used it yet.

A Quick Introduction to the Django Template System

Once you've finished reading this you might want to head over to Jeff Croft and read his post on default templates.

16 comments:

Gerry said...

I can't get my first flat page to display :-). I get a 404 error 'No FlatPage matches the given query.' Skipping on to Chpt 3 I added tinyMCE without a problem.

I have added
(r'', include('django.contrib.flatpages.urls')),
to urls.py. In settings.py I have
TEMPLATE_DIRS = (
'D:/django/django-templates/cms/',
)

and I added the following files:
D:\Django\django-templates\cms\flatpages\default.html
D:\Django\django-templates\cms\admin\flatpages\change_form.html

Whether I have default.html or not I get the same error. I have tried various paths but always get the same 404 error. Any ideas?

Gerry
(Python 2.6, Django 1.0.2 under Win32)

Brett said...

Gerry,

Your 404 suggests it is not a problem with the templates. I would go back and double check that you put in the 'sites' entry for localhost 127.0.0.1:8000 and make sure your first-page is added for that site rather than example.com. Try adding some other pages and see whether you can get them to display.

Gerry said...

Tried that. No joy.
Looked at the database - looks OK.
I had added some eggs so I reinstalled Python in case one of them was messing it up.
I tried relocating the database.
Argh!

Brett said...

Gerry,

zip up all your cms files in the structure they are and send them to me at brett 'at' haydon.id.au - I'll have a look in my XP virtual machine.

Cody said...

i just had this problem. i had to add something to middleware in settings to manage the flatpages.

here it is:

'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',

Brett said...

Updated the post to reflect the Sites feature/gotcha which caught Gerry out, and the middleware addition - thanks Cody.

This has caught me out a couple of times as well but I forgot about it when writing this post.

Gerry said...

There's always one gotcha... Many thanks for sorting that one out Brett.

Greg said...

The example in the book does not need the middleware component so I think it is better not to mention it. Then you do need the:
(r'', include('django.contrib.flatpages.urls')),
line.

The paragraph beginning "On page 15" is key. You must add the page to the example.com URL but to view the page you must browse to http://127.0.0.1:8000/first-page/. http:://example.com/first-page doesn't work. This is confusing. If you don't do this you will get a "No FlatPage matches the given query." error

Jim N said...

I'm having the same problem as Gerry, and adding the middleware settings as Cody suggests has no effect.

Just putting that here for anyone else who's stuck.

Jim N said...

AH, fixed it by rereading. I'd set my SITE_ID to 2. Seems this library only works when SITE_ID is 1 in settings.py.

darkdisaster said...

OMH Jim N :D
How did you discovered that 'bug'?
Thanks so much... that bug was annoying me for 2 days...
How would I be able to remember checking if there's a site_id in settings.
Thanks so much! :D

iamairborne said...

I created another and did not example.com.did not change site_id either after that.Thank you for writing about the site_id.

Darren said...

hey i found this post very helpful. I too had deleted the example website and so I wasn't able to go on with the tutorial. Reading about the site id saved me! Thanks again!

calakan said...

Okay, i have the same problem with chapter 2 of the book. because i'm trying it in real server (not python manage runserver) i got the same error over and over, eventhough i have the correct domain... can u help me pls?

my domain is django.jasait.com
my flatpage si /pucing/
i have try over2 look in urls.py and settings.py still no clue and i use mysql as database...any idea?

sujiro said...

Simple CMS was designed to meet the needs of real business people. It was not designed by developers for other developers like the open source systems available and it was not based on somebody's perception of what business needed, based on their own experience

nima said...

Hi every body

I have same problem

I set SITE_ID to 1
and also delete example.com

but still same problem :(

Hedged Down