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

The original reference docs.djangoproject.com, what can manage.py utility do? find here.

1.Database Setup

Edit the settings.py 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 manage.py syncdb

2 set Time Zone

The default for the United States Central time Chicago

3.Installed_apps

The default settings.py 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 manage.py syncdb to create the DB file. this command database configuration information is given to find the information in the installed_apps and settings.py 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 manage.py startapp polls; the corresponding polls folder has __init__.py, models.py, tests.py, views.py 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 model.py 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[5.settings.py polls],

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

    

  The 7 option: the database configuration using the manage.py,

  8.Run: manage.py syncdb create model tables in the database,

    

  9.Run:manage.py shell into the shell, then you can use the Database API operation was defined by models(CRUD);

This demo from the original

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