Open-source Web application framework Django graphic tutorial

Source: Internet
Author: User
Tags install django
Python has many different Web frameworks. Django is the most representative heavyweight player. Many successful websites and apps are based on Django. Django is an open-source Web application framework written in Python. Next let's take a step-by-step learning. This article introduces the open-source Web application framework Django graphic tutorial.

Python has many different Web frameworks. Django is the most representative heavyweight player. Many successful websites and apps are based on Django. Django is an open-source Web application framework written in Python. Next, let's study it step by step.

This article is intended for beginners who are new to the web framework and have a basic knowledge of python.

Environment: Windows 7 python3.5.1 pycharm professional Django 1.10 pip3

1. Introduction to Django

Baidu Encyclopedia: open-source Web application framework written in Python ......

Key point: a big and comprehensive framework is all taken into consideration for you.

1. web framework introduction

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.

Common 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:


So what do we learn from Django?

1. Directory Structure Specification

2. URL routing

3. settings configuration

4. ORM operations

5. Template rendering

6. others

2. Django project instance

1. program installation

Python3.5, pip3, and pycharm Professional Edition are independently installed.

(1) install Django:

Here we will only introduce the simple installation method of the pip3 command.

Win + r, call out cmd, run the command: pip3 install django, automatically install the latest version provided by Pypi.

After the installation is complete, as shown in:

(2) configure the system environment

After successful installation of django, you can find the django-admin.exe file in the middle and add it to the operating system environment variable. In this way, future calls will be more convenient.

Run: django-admin help. The following content indicates OK.

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, we recommend that you use pycharm, which is currently the best python development IDE.

It has powerful functions and user-friendly interfaces. (All the following operations are performed in pycharm .)

Click file --> new project. the following dialog box is displayed.

Select the Django topic and enter the project name. here, mysite, which adopts international conventions, is used. Select the python interpreter version and click create.

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. 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.

Enter the command in the terminal below pycharm:

Python manage. py startapp cmdb

In this way, an APP called cmdb is created, and django automatically generates the "cmdb" 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.

Command line: python manage. py runserver 127.0.0.1: 8000

But in pycharm, you can do this:

In the upper toolbar, find the icon shown below.

Click the drop-down arrow

Click edit configurations.

Enter 127.0.0.1 port in host: 8000

OK. click the green triangle to run the web service.

As shown in the figure, the browser program page is automatically displayed. The displayed page is 404:

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 ".

Note: Here is a tip: when the service is frequently restarted, it is easy to start the service because the port is not released. if you modify the port, it will be OK.

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 pycharm.

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 its own template language, similar to jinja2. based on the provided data, it replaces the corresponding part of html. 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 about using databases. django uses its own ORM framework to operate databases and comes with a lightweight sqlite3 database. Let's take a look:

First, register the app:

If you do not register it, your database does not know which app to create a table.

Then, in settings, configure database-related parameters. if you use the built-in sqlite, you do not need to modify the parameters.

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, run the command in teminal of pycharm to create a database table. There are two commands:

Python manage. py makemigrations

Run python manage. py 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.

Suggestions on learning methods: Do not add details directly to learning anything. you should first understand its peripheral knowledge, look at its overall architecture, and then learn its basic content, then, you can learn and polish your skills!

The above is the details of the Django graphic tutorial of the open-source Web application framework. For more information, see other related articles in the first PHP community!

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.