Friday, June 11, 2010

Django - Basics

Updated 2010-08-07

Django is a Web framework that allows making webs in a fast and easy way. As his lemma says, it is a framework "for perfectionists with deadlines".

Here there is a basic tutorial, divided in four steps, which explains its fundamentals.
  • Step 1. Creating the data Model.
  • Step 2. Activating the automatic web Administration.
  • Step 3. Creating the public interfaces - Views.
  • Step 4. Form processing and depurating the code.
Django structures the code in three parts, following the classic Model-View-Controller scheme, although it is slightly different. Its parts are the following:
  • Model, which contains the relational scheme of the database.
  • Views, which deploy the web functionalities.
  • Templates, which concrete the final HTML code which is seen in the web application.
The basic files and folders in which a Django application is divided are the following:
  • settings.py File with configuration parameters
  • manage.py Application which allows to run the development server, to reset the database and other functionalities.
  • urls.py Module which works as an URL dispatcher.
  • application/models.py Contains the Model of the application.
  • application/views.py Contains the Views of the application.
  • application/forms.py Contains the forms used in Views and Templates.
  • application/admin.py Admin default module.
  • templates/ Contains different Templates.
  • files_media/ Contains static files like images, CSS or JavaScript files
Some typical problems when working with Django:

Modification of the Model
  • Once the model is created, we may need to change it, changing tables, adding new ones, and so on.
  • If we allow to loose the existing data, the easiest way to do that is the following, explained in the answer 10 of the FAQ's: to delete the existing tables with the command
manage.py reset appname
and re-creating them with
manage.py syncdb
manage.py sql appname
  • If we do not want to loose the existing data we must be more careful:
    • If we only add tables without modifying or deleting other tables, the application of the re-creating commands works well.
    • If we also want to modify an existing table we must be more careful: We must modify manually the database. A good way to do that is with the alter table command of sqlite3. We must be very careful and we must maintain in each moment the integriity between the database and the defined model:
      • Fields names: The field names of the table finish with _id.
      • Fields are not null by default at the model and null by default in the database. So, it is not a problem. If we want the database to store not null fields, we must indicate it:
CREATE TABLE "myapp_informacio" (
"id" integer NOT NULL PRIMARY KEY,
"usuari_id" integer NOT NULL
REFERENCES "myapp_usuari" ("id"),
"missatge" varchar(250) NOT NULL,
"data_consulta" datetime NOT NULL
)
      • When we want a field to be not necessarily null at the database we must modify the model adding the "restrictions":
camp = models.CharField(null=True,
blank=True,max_length=5)

No comments:

Post a Comment