Simple Django tutorial, django tutorial

Source: Internet
Author: User
Tags install django django tutorial

Simple Django tutorial, django tutorial
1. Introduction to Django 1. Introduction to the web Framework

Before introducing Django, you must first introduce concepts such as WEB framework.

Web Framework: someone else has set a web website template. You can learn its rules and then "fill in" or "modify" it to what you need.

The general architecture of the web framework is as follows:

Other python-based web frameworks, such as tornado, flask, and webpy, are added, deleted, and cropped in this range. For example, tornado uses its own asynchronous non-blocking "wsgi", and flask only provides the most streamlined and basic framework. Django directly uses WSGI and implements most of the functions.

2. Introduction to MVC/MTV

  MVC Baidu encyclopedia: Full name Model View Controller, short for model-view-controller, a software design Model, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic.

  General Explanation: A file organization and management form! Don't be scared by the abbreviations. This is actually a way to put different types of files in different directories, and then get a tall name. Of course, it brings many benefits, such as frontend and backend separation and loose coupling, which are not described in detail.

Model: defines database-related content, which is generally stored in the models. py file.

View: defines HTML and other static webpage files, that is, html, css, js, and other front-end things.

Controller: defines business logic. It is your main code.

  MTV: Some WEB frameworks have changed the literal meaning of MVC. View is not HTML-related, but the main business logic, which is equivalent to a controller. Html is put in Templates, called a template, so MVC becomes MTV. This is actually a text game. It is essentially the same as MVC. It only changes the name and name, but it does not change the name.

3. Django MTV model organization

Directory separation requires a mechanism to couple them in the same directory. In Django, urls, orm, static, and settings play an important role. A typical business flow is shown in:

2. Django project instance 1. Program Installation

Python3.5, pip3, and pycharm Professional edition are independently installed. Pycharm does not use the free version. It does not support Django.

(1) install Django:

Http://www.cnblogs.com/qianyuliang/p/6729298.html

2. Create a django Project

On the command line interface such as linux, projects can also be developed using commands and vim provided by django. However, eclipse is used here

Click file --> project. The following dialog box is displayed.

Select the PyDev/Django topic and enter the project name. Here, mysite, which adopts international conventions, is used.

Next is enough.

Django will automatically generate the following directory structure:

The configuration file is in the directory with the same name as the project, and the templates directory is html file storage, that is, T in MTV (manually created ). Manage. py is a django project management file.

3. Create an APP

Each django project can contain multiple apps, which are equivalent to subsystems, submodules, functional components, and so on in a large project. They are independent of each other, but they are also related.

All Apps share project resources.

Right-click mysite ---> Django ---> Create application

In this way, an APP called app01 is created, and django automatically generates the "app01" folder.

4. Write routes

The routes are all in the urls file, which maps the url entered by the browser to the corresponding business processing logic.

Simple urls compiling methods are as follows:

5. Write the business processing logic

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

Through the above two steps, we point the index url to the index () function in views, which receives user requests and returns a "hello world" string.

 

6. Run the web Service

Now we can run the web service.

Remember to write app01 to settings. py.

 

Command Line: python manage. py runserver 127.0.0.1: 8000

In eclipse, ---> run deployments

If this prompt is displayed, it indicates that the startup is successful. Open the browser and enter 127.0.0.1: 8000.

Modify the url and add "/index". Everything is OK!

So far, a web Service compiled by django is successfully started.

7. Return HTML files

What did we return to the user's browser? A string! In fact, this certainly does not work. We usually return html files to users.

Below, we write such an index.html file:

Modify the views file:

To let django know where our html file is, modify the corresponding content of the settings file. But by default, it works well and you do not need to modify it.

 

Next, we can restart the web service. Refresh the browser and you will see the "hello world" style ".

8. Use static files

We can already return html files to users, but it is not enough. There are three front-end sections: html, css, js, and various plug-ins. They are complete.

. In django, static files are usually stored in the static directory. Next, create a static directory in mysite.

Your CSS, JS, and various plug-ins can all be placed in this directory.

To make django find this directory, you still need to configure settings:

Similarly, in the index.html file, you can introduce the js file:

 

Restart the web service, refresh the browser, and view the results.

9. Receive user data

Above, we returned an html file with complete elements to the user's browser. However, this is not enough because there is no dynamic interaction between the web server and the user.

Below we design a form for the user to enter the user name and password and submit it to the index url. The server will receive the data.

First modify the index.html File

 

Modify the views. py file.

At this point, an error occurs when you restart the web service. Because django has a cross-site request protection mechanism, we disable it in the settings file.

Enter the browser again and refresh the page:

Enter something, and then we can see the corresponding data in eclipse.

10. Return to the dynamic page

We have received user data, but it is still a static page that is returned to the user. We usually process the data and then return it to the user.

At this time, django uses the jinja2 language to write a dynamic template. jinja2 will replace the corresponding part of html based on the provided data. After getting started with the detailed syntax, we will continue to learn more.

First, modify the views. py file:

Modify the index.html file:

Restart the service and refresh the browser:

We can see that we have obtained real-time user input data and displayed it on the user page in real time. This is a good interaction process.

11. Use the database

Here, the django MTV framework has basically surfaced, leaving only the final database.

Although we interact well with users, we did not save any data. Once the page is closed or the server is restarted, everything will return to the original state.

There is no doubt that mysql Data is used below.

In settings, configure database-related parameters. If you use the built-in sqlite, you do not need to modify the parameters. Create a mysite database in the mysql database.

Edit the models. py file, that is, M in MTV.

 

Here we have created two fields to save the user name and password respectively.

Next, you need to create a database table through commands in the background. There are two commands:

Python manage. py makemigrations

Run python manage. py migrate.

Alternatively, in eclipse, right-click mysite ---> django ---> makemigrations

Right-click mysite ---> django ---> migrate

Modify the business logic in views. py

After the web Service is restarted, refresh the browser page and save the data that interacts with the user to the database. Data can be read from the database at any time and displayed on the page.

At this point, a complete set of elements and clear django project display by the main framework are completed. Is it actually very simple?

Iii. Django Summary

As a web Framework for python, Django has powerful functions and comprehensive content. However, it also means a lot of restrictions, low flexibility, and poor modification. This is why the fish and the bear's paw cannot be used at the same time. To learn about Django, we actually need to learn a software. We need to understand its basic principles, grasp its overall framework, and keep in mind some basic rules. The rest is to constantly go into details, then, the questions about how practical and experienced people are, and there is no advanced technology that cannot be mastered.

Related Article

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.