Study of Django ----settings and database_based App demo

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The original reference, what can utility do? find here.

1.Database Setup

Edit the file, the configuration database, including provider and database (file) name, and then save the (not sure if compile). If SQLite, use this operation to automatically create the database file (given the absolute path name C:/blah/blah/demoDB.db, engine:'django.db.backends.sqlite3')

Use: Python syncdb

2 set Time Zone

The default for the United States Central time Chicago


The default file at the bottom of a few apps:

  PS: above app each at least using a database table, so it is necessary to use Python syncdb to create the DB file. this command database configuration information is given to find the information in the installed_apps and to create the database and table. When commenting out some of the default apps, this command to create a table. for not comment in installed_apps apps

  A app through the package and distribute operations, its use in multiple project.

3.Create models(with an app demo called Polls)

  Every written using the Django app is a Python package, according to the agreement will have the corresponding path in Python. The app directory structure by Django own utility to complete, we only need to code focus.

  The difference between app and project: personal understanding, equivalent to the difference of VS under project and solution. A app can belong to more than one project, one project can contain multiple apps and the related configuration file.

  The project can be on any path of python, such as: Python startapp polls; the corresponding polls folder has,,, four files.

  1 create database based on web app first to define models (data layout, including fields and data operation); Django also follow the DRY principles, the one definition, multiple inheritance,

  2 create two model, each Choice is associated with a Poll, edit the app folder under the file, as follows:

    from django.db import models

    class Poll(models.Model):

    question = models.CharField(max_length=200)

    pub_date = models.DateTimeField('date published')

    class Choice(models.Model):

    poll = models.ForeignKey(Poll)            #A foreign key, Django support many-to-ones,many-to-manys,one-to-ones.

    choice_text = models.CharField(max_length=200)  #Field class type, certain types of parameters are needed for dual purpose: DB schema and validation.

    votes = models.IntegerField(default=0)         #Choice_text and votes to store the field name in dB, can use the value in Python.

    The PS: of each model is represented by a inherits from the models.Model class, each model has several variables, each variable represents a database field in model,

  Definition 3 above for Django: 1) to create database schema for app (create table statements); 2) to create the Pyhton used to access the Poll and Choice objects in API,

  4 the first to tell project, polls app is already installed. Django apps is a plug-in type (pluggable), a app can be used for multiple project (as showed above), we can distribute the apps, because the apps is not coupled to other Django installation (personal understanding is not coupled to any project),

  Addition of the app[ polls],

  6.Run: python sql polls, Tell Django contains polls app:


  The 7 option: the database configuration using the,

  8.Run: syncdb create model tables in the database, shell into the shell, then you can use the Database API operation was defined by models(CRUD);

This demo from the original

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ella at November 12, 2013 - 5:43 PM