Django scaffolding

Posted on by Dariusz Fryta

We all love Django — a simple but powerful framework. However, Ruby on Rails has at least one (and few others I’m sure!) advantage: scaffolding. It allows developers to create the models, views, and templates in a single operation. It’s an interesting solution — you don’t have to manually create all the things that usually take a lot of time. So why don’t we have this feature in Django? Well… now we do!

Our latest django-common 0.3 comes with a scaffolding feature!

Overview

Here is what django-common scaffold can do:

  • - create app,
  • - create models,
  • - create views,
  • - create templates,
  • - create forms,
  • - create urls,
  • - create tests.

Scaffold creates the app (similar to startapp that ships with Django), models with fields, CRUD views with ajax forms, prepares templates, fills urls, and creates CRUD tests with only one command!

Installing

Use pip installer:

Or just download/clone directly from github:

https://github.com/Tivix/django-common

Then add it (django_common) to INSTALLED_APPS and set up SCAFFOLD_APPS_DIR in settings.

Default is set to main app directory. However if you use django_base_project you must set up this to SCAFFOLD_APPS_DIR = ‘apps/’.

Run scaffold, run!

To run scaffold type:

APPNAME is the name of the new application. If application already doesn’t exist it will be created.

MODELNAME is the name of the model. Just enter model name that you want to create (for example: Blog, Topic, Post etc). It must be alphanumerical. Only one model per run is allowed!

[fields] – list of the model fields.

Field types

Available fields:

All fields requires name that is provided after : sign, for example:

Two fields, foreign and decimal require additional parameters:

- “foreign” as third argument takes foreignkey model, for example:

NOTICE: All foreign key models must already exist in project. User and Group models are imported automatically.

- decimal field requires two more arguments max_digits and decimal_places, for example:

NOTICE: To all models scaffold automatically adds two fields: update_date and create_date.

Templates

Scaffold templates use two blocks extending from base.html:

So be sure you have your base.html set up accordingly.

Example usage

Let’s create a very simple forum app. We need Forum, Topic and Post models.

Forum model
Forum model needs just one field name:

Topic model
Topics are created by site users so we need: created_by, title and Forum foreign key (update_date and create_date are always added to models):

Post model
Last one are Posts. Posts are related to Topics. Here we need: title, body, created_by and foreign key to Topic:

All data should be in place!

Now you must add forum app to INSTALLED_APPS and include app in urls.py file by adding into urlpatterns:

Now syncdb new app and you are ready to go:

Run your server:

And go to forum main page:

All the structure is in place. Now you can personalize models, templates and urls.

At the end you can test new app by running test:

Happy scaffolding!

This entry was posted in development, django, scaffolding, tutorials and tagged , , , , , . Bookmark the permalink.
  • Brent

    Pretty slick. I look forward to giving it a try this weekend.

Tivix

A full service boutique design and technology firm specializing in developing dynamic solutions which help organizations to engage consumers, and build relationships in unique, powerful new ways.

What We Do... why we're different!

We build high-engagement web, mobile, and social apps. We build them for startups, established brands, nonprofits, and organizations of all kinds. The Tivix team brings a unique combination of savvy designers, rock-solid engineers, and deep business experience.

Facebook