Django Web Development Introductory Tutorial (II)

Source: Internet
Author: User

Create models

Every app written by Django contains a Python package that follows a specific convention. Django includes a tool that automatically generates a basic directory structure, all without having to create a directory, just focus on writing code.

Database of public records or a simple poll app. A project is a collection of the configuration and apps for a particular Web site. A Project
can contain multiple apps. An app can is in multiple projects.
Note: Apps is a Web application that can be a blog system, a simple voting system. A project is a collection of configurations and applications (apps). Project can contain multiple apps. An apps can be pure in multiple project.

To create an apps, make sure the current directory and are in the same directory, and type the following directives:

$ python Startapp polls
polls/    migrations/

The first step in writing a database Web app is to define your models (essentially, the metadata design of the database).

Essentially just a history this Django can roll through to update your the database schema to match your current models.

In our simple poll app, we will create two models:Question and Choice. Question has a question and publication date. Choice has two fields: Choice text and ballot statistics. Each of the choice is associated with a question.

These concepts are represented by a simple Python class. Edit the polls/ file as follows:

 from Import Models class Question (models. Model):    = models. Charfield (max_length=200)    = models. Datetimefield ('date published')class  Choice (models. Model):    = models. ForeignKey (Question)    = models. Charfield (max_length=200)    = models. Integerfield (default=0)

The code above is very intuitive. Each Model is represented by a Django.db.models.Model subclass. Each model has some class variables, each representing the fields of the database.

Each field was represented by an instance of aFieldClass–e.g.,CharfieldFor character fields andDatetimefieldFor DateTimes. This tells
Django what type of data each field holds. The name of eachFieldinstance (e.g.Question_textOrpub_date) is the field ' s name, in machine-friendly format. You'll use the this value in your
Python code, and your database would use it as the column name. You can use an optional first positional argument to aFieldTo designate a human-readable name. That's used in a couple of introspective parts of
Django, and it doubles as documentation. If This field isn ' t provided, Django would use the machine-readable name. In this example, we ' ve only defined a
Human-readable name forquestion.pub_date. For all and the field ' s machine-readable name would suffice as its human-readable name. SomeFieldClasses has required arguments.Charfield, for example, requires so give it aMax_length. That's used not only in the
Database schema, but in validation, as we'll soon see. AFieldcan also have various optional arguments; In this case, we ' ve set thedefaultValue ofvotesTo 0.Finally, note a relationship is defined, usingForeignKey. That's tells Django eachChoiceis related to a singleQuestion. Django supports all the
Common database Relationships:many-to-one, Many-to-many and one-to-one.
Activating models

Such a small piece of code provides a lot of information for Django. With it, Django can do these things:

    • Create a database structure (schema, create TABLE statements)
    • Create a Python database API that accesses Question and Choice objects

But the first thing to do is tell our project that the polls app will be installed.

Philosophy  Django apps is "pluggable": You can use a app in multiple projects, and you can distribute apps, because they don ' t has To is tied to a given Django installation.

Edit the file mysite/ and modify the Installed_apps settings, including the string ' polls '. As shown below:

Installed_apps = (    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'polls',)

Django Web Development Introductory Tutorial (II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.