Django First Project

Source: Internet
Author: User
Tags web services install django in python
First, Django introduction

Baidu Encyclopedia: Open source Web application framework, written in Python language ...

Focus: A chatty frame, everything for you to think about. 1. Introduction to the Web framework

Before you introduce Django, you must first introduce concepts such as Web frameworks.

Web framework: Someone else has set up a Web site template, you learn its rules, and then "fill in the blanks" or "modify" the way you want.

The architecture of the general web framework is this:

Other Python-based web frameworks, such as tornado, flask, and webpy, are cut and deleted within this range. For example, Tornado uses its own asynchronous non-blocking "Wsgi", flask only provides the most streamlined and basic framework. Django uses WSGI directly and implements most of the functionality. 2. MVC/MTV Introduction

MVC Baidu Encyclopedia: Full Name Model View controller, which is the abbreviation of models-View-controller, a kind of software design model, organizes the code with a method of business logic, data, interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction.

Popular explanation: A form of organization and management of a document. Don't be intimidated by abbreviations, this is a way to put different types of files in different directories, and then take a tall name. Of course, it brings a lot of benefits, such as front-end separation, loose coupling and so on, it is not explained in detail.

Model: Define database-related content, typically in a models.py file.

View: Defines static Web page files such as HTML, that is, HTML, CSS, JS and other front-end things.

Controller: Define business logic related, which is your main code.

MTV: Some Web frameworks think that MVC's literal meaning is awkward, just change it for a bit. View is no longer HTML related, but the main business logic, equivalent to the controller. HTML is placed in templates, called a template, so MVC becomes MTV. This is actually a word game, and MVC is essentially the same, changed a name and called, the new. the 3.Django MTV model organization

Directories, there must be a mechanism for them to be coupled inside. In Django, URLs, ORM, static, settings, and so on, play an important role. A typical business process is shown in the following figure:

So what we learn from Django.

1. Directory Structure specification

2. How URLs are routed

3. Settings configuration

4. ORM Operation

5. Template rendering

6. Other two, Django Project example 1. Program Installation

python3.5, PIP3 and Pycharm Professional editions are installed on their own. (1) Installing Django:

This is only a simple way to install the PIP3 command.

Win+r, call up cmd, Run command: PIP3 install Django, automatically installs the latest version provided by PyPI.

After the installation is complete, the following figure shows:

(2) Configuring the system environment

After the successful installation of Django, the path in the following figure can be found in the Django-admin.exe file and added to the operating system environment variable. This will be more convenient for subsequent calls.

Run: Django-admin help, can see the following content indicates OK.

2. Create a Django project

Under the command-line interface like Linux, you can use the commands and vim provided by Django to develop your project. However, it is recommended to use Pycharm, the best python development IDE in the present

, it is powerful and user friendly. (All of the following operations are performed in Pycharm.) )

Click: File–>new Project, the following dialog box appears.

Select the Django column and enter the project name, which uses the MySite of international practice. Select the Python interpreter version and click Create.

Django will automatically generate the following directory structure:

The directory with the same name as the project is a configuration file, and the templates directory is the HTML file that is the T in MTV. Manage.py is a Django project management file.

3. Create an app

You can include multiple apps in each Django project, like sub-systems, sub-modules, features, and so on, in a large project, independent of each other, but also associated.

All apps share project resources.

Enter the command in the terminal terminal below Pycharm:

Python manage.py Startapp CMDB

This creates a app,django, called the CMDB, that automatically generates a "CMDB" folder.

4. Writing a route

The route is in the URLs file, which maps the URL entered by the browser to the appropriate business processing logic.

The simple way to write URLs is as follows:

5. Writing Business processing logic

The business processing logic is in the views.py file.

With the above two steps, we point the index URL to the index () function in views, which receives the user request and returns a "Hello World" string. 6. Run the Web service

Now we are ready to run the Web service.

The command line is in the following way: Python manage.py runserver 127.0.0.1:8000

But in Pycharm, you can do this:

In the Upper toolbar, locate the icon shown below.

Click the drop-down arrow

Click Edit Configurations

Fill in Host: 127.0.0.1 Port: 8000

After OK, click on the green triangle and the Web service will run.

Automatically jump to the browser program interface as shown in the figure. The 404 page shown is the following figure:

Modify the URL, add "/index", everything OK.

At this point, one of the simplest Django-authored Web services started successfully. 7. Return HTML file

What we return to the user's browser above. A string. In fact, this is certainly not possible, usually we are returning the HTML file to the user.

Below, we write such a index.html file:

Then modify the views file:

In order for Django to know where our HTML files are, we need to modify the corresponding contents of the settings file. But by default, it just works, and you don't have to modify it.

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.