Actual combat Django: official Example Part1

Source: Internet
Author: User

"Write in front."

Writing this actual series of Django articles, is a long time ago some ideas, the problem is too few instances on hand, once the lecture, fear of "no rice pot" worry.

With the deepening of the Django learning, and gradually have some experience, put these ideas, to standardize, convenient to write the way to get started by writing this combat series, I believe that just contact with Django's children's shoes will have a certain help.

Willing to use the version here, for the Django 1.7.1,python 3.2, want to follow the example of willing to practice, please be sure to use the version consistent.

The instance is debugged under Windows, and if you are using a non-Windows system, try to adjust it yourself, depending on the difference in the operating system.

"Django and Python Installation"

For the installation of Django and Python, please read the official documentation.

The IDE recommends using ERIC5.

"About this instance"

The examples here are from the official 1.7.1 version of the document and should be the only complete instance of the official documentation. Willing to tidy up this instance, did not follow the official documents in order to explain, but in accordance with the logic of the station to write down, omit the individual steps and operations, such as "Playing with the API", is speaking in the Shell interface play database, Interested children's shoes can check the official documents themselves.

The original example is divided into 6 part, willing to explain the time, will upset the original part structure, just according to their own logic to explain. For example, this article "Part1", in fact, has introduced the official document "Part2" part of the content. You can skip this article to read the official documentation for children's shoes that are willing to take care of this approach.

1. Create a project

After the Django 1.7.1 version is installed, we can find the "Django-admin.exe" program under Python's scripts, and when creating a new project, go to the Scripts folder at the DOS command prompt (such as "c:\python32\ Scripts "), and then run the following command:

$ django-admin Startproject MySite

Note: $ represents a DOS prompt and does not have to be entered (hereinafter).

When the above command finishes executing, a folder called MySite is added to the Scripts folder with the following structure:

mysite/    manage.py    mysite/        __init__.py        settings.py        urls.py        wsgi.py

In these documents, the settings.py and urls.py are the two documents that we will use frequently in the future. Each document is meant to be represented, and can be found in the official documentation. The idea is that you don't have to take care of that much, and follow this set of "Live Django" tutorials in one instance, and you'll naturally know what each document is used for.

2. Create an App

About the difference between the project and the application, willing to simply talk about their own understanding. The project is like a website, the application is only a part of the site, it is used to complete certain functions, such as a website can be divided into "blog", "Forum", "library" and other applications.

We are now going to create a voting application, as follows:

Execute at the DOS command prompt:

CD MySite

Go to the MySite folder that we just built, and then run the following command:

$ python manage.py Startapp polls

Note that the Python installation path should be added to the environment variables of the operating system, and if the above command cannot be run, the relative path can be used, and the command can be adjusted to ". \.. \python manage.py Startapp Polls ", this issue is no longer discussed below, please add Python to the system environment variable as soon as possible.

After the above command is executed, a folder called polls is added to the MySite folder with the following structure:

polls/    __init__.py    admin.py    migrations/        __init__.py    models.py    tests.py    views.py

3. Building a model

Model (models) is the data source for our application. In Django, all we have to do is edit the following models.py file:

Edit the polls/models.py file as follows:

polls/models.py:

 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)

Here the two class, equivalent to tell the database engine, will be to me to build these two tables, the first table is called question, there are two fields, the second table is called choice, there are three fields . In fact, the database engine automatically adds an incremented ID field to each table when the data table is created.

4. Activating the Model

First of all , to modify the mysite/settings.py This file, find the Installed_apps this setting, change it to look like this:

mysite/settings.py:

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

Actually is to tell the program, I want to add this polls application!

When editing settings.py this document, modify the time zone and language settings to find:

' en -US '

Change it to:

' ZH-CN '

Found it:

' UTC '

Change it to:

' CCT '

Note: CCT is the time zone abbreviation for Beijing, China. It is recommended that you make changes to the time zone and language settings in each of these projects.

Then run the following command at the DOS command prompt:

$ python manage.py makemigrations polls

You will see an execution result like this:

 for ' polls ' :  0001_initial.py:    - Create model Question    - Create model Choice    -ADD Field question to Choice

Continue to run the following command at the DOS command prompt:

$ python manage.py Migrate

You will see an execution result like this:

Operations to perform:  Synchronize unmigrated apps:sessions, admin, messages, auth, staticfiles, contenttypes  Apply all Migrations:pollssy Nchronizing Apps without migrations:  Creating tables ...  Installing custom SQL   ...  from 0 Fixture (s) Running migrations:  Applying polls.0001_initial ... OK

Remember Makemigrations and migrate these two commands, it is used to create or modify the database, very important!

5. Create an Administrator account

Run the following command at a DOS command prompt:

$ python manage.py createsuperuser

Enter a user name, suggested input: admin:

Username:admin

Then enter the administrator's mailbox, find your own mailbox input is:

[Email protected]

Next enter the password for the administrator two times:

Password: *******************Superuser created successfully.

See "Superuser created successfully" is done.

Usually each project is built to do this step of "Create Administrator account", so please keep in mind.

6. Start the server

Everything is ready, only owed the east wind, the front we have put the framework of the site up, and now the server started up to see the effect.

Run the following command at a DOS command prompt:

$ python manage.py runserver

After the command executes, you will see a character like this appear:

December, 2014-00:30:53'mysite.settings'starting development Server at http://127.0.0.1:8000/Quit the server with CTRL-break.

This means that the server has successfully started.

Note: Runserver is a very important command, but does not need to be deliberately written, in the process of doing the example, this may be you remember the most prison command.

OK, open the browser, enter in the Address bar:

http://127.0.0.1:8000/admin/

You will see the following page:

This is a great benefit of using Django and Django comes with a powerful management background. Now, just log in with the administrator account you just built!

The post-login page will look like this:

Is there anything we can do now, in addition to user and user group management? Do not worry, willing to be in the next section to teach you how to add content inside a little bit.

"Not to be Continued"

Actual combat Django: official Example Part1

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: info-contact@alibabacloud.com 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.