Diagram Python "12th": Django Basics

Source: Internet
Author: User
Tags administrator password

This section contains a list of:

Django Basics: http://www.ziqiangxuetang.com/django/django-tutorial.html

First, Django introduction

Django File Description: http://www.cnblogs.com/geekmao/p/7569950.html

1.web Framework Introduction

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

The flask provides only 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 for models- View- Controller A software design paradigm that organizes code with a separate approach to business logic, data, and interfaces, and aggregates business logic into a single component, without the need to rewrite business logic while improving and customizing the interface and user interaction.

  Popular explanation : a form of file organization and management! 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 as follows:

Django Project main py file
File Function
urls.py URL entry, associated to the function for the views
models.py Related to database operations, establishing an application data model
views.py Process the user to make a request, which corresponds from the URLs, by rendering the contents of the page in the templates.
settings.py Related settings, including database settings, mail settings, static file configuration, etc.
forms.py form, the form data class submitted by the user on the browser side
admin.py Background code, mostly completed

Version of Python for the Django version:

Django version Python version
1.8 2.7, 3.2, 3.3, 3.4, 3.5
1.9, 1.10 2.7, 3.4, 3.5
1.11 2.7, 3.4, 3.5, 3.6
2.0 3.5+

Ii. Examples of Django projectsinstallation of 1.Django

Http://www.runoob.com/django/django-install.html

Http://www.runoob.com/django/django-first-app.html

Http://www.cnblogs.com/feixuelove1009/p/5823135.html

2. Create a Django project2.1, the way to use the command is: PythonDjango-admin. PY Startproject Project name 2.2, the following is the way to use the interface:

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.

Simple ways to write URLs such as:

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. Yes, it is. 404 Pages:

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

Next, we can restart the Web service. In the browser refresh, you will see the style of "Hello World".

Note: Here is a small trick, in many times to restart the service frequently, because the port is not released because of the reason, easy to start the service, modify the port is OK.

8. Using static files

We can already return the HTML file to the user, but not enough, the front-end three chunks, HTML, CSS, JS and a variety of plug-ins, they are complete is a full

The page. In Django, static files are generally placed in the static directory. Next, create a new static directory in the MySite.

Your css,js and various plugins can be placed in this directory.

In order for Django to find this directory, settings still needs to be configured:

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

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

9. Receiving the data sent by the user

Above, we return a full-featured HTML file to the user's browser. This is not enough, however, because there is no dynamic interaction between the Web server and the user.

Below we design a form, let the user enter user name and password, submit to index this URL, the server will receive this data.

Modify the index.html file first

Then modify the views.py file

At this point, when restarting the Web service, an error occurs because Django has a cross-site request protection mechanism, and we close it in the settings file.

Once again, go to the browser and refresh the page:

Input something, and then we can see the corresponding data in the Pycharm.

10. Return to the dynamic page

We received the user's data, but the return to the user is still a static page, usually we will be based on the user's data, processed and returned to the user.

At this point, Django uses its own template language, similar to JINJA2, to replace the corresponding portions of the HTML with the data provided, and to learn more about the syntax.

First transform the views.py file:

Re-transforming the index.html file:

Restart the service and refresh the browser:

As you can see, we get the data that the user has entered in real time, and it's a good interactive process to show it in real time on the user's page.

11. Using the database

Process comes here, the Django MTV framework has surfaced, leaving only the last database part.

Above, although we interact with the user very well, but did not save any data, the page once closed, or the server restarts, everything will go back to the original state.

Without a doubt,Django operates the database with its own ORM framework and comes with its own lightweight sqlite3 database . Let's take a look at the following:

First, register the app:

Without registering it, your database will not know which app to create the table for.

Then we configure database-related parameters in settings, and if you use SQLite with your own, you don't need to modify it.

Then edit the models.py file, the M in MTV.

Here we create 2 fields, each saving the user's name and password.

The next step is to create a table of the database in Pycharm's teminal by command. There are 2 commands, namely:

Python manage.py makemigrations

Re-enter command: Python manage.py migrate

Modify the business logic in views.py

After the Web service is restarted, the browser page is refreshed, and then the data that interacts with the user can be saved to the database. Data can be read from the database at any time and displayed on the page.

At this point, a complete element, the main framework to show a clear Django project completed, is actually very simple is not it?

Iii. Summary of Django

As Python must be the web framework of Django, it's powerful, comprehensive content, but also means that there are a lot of restrictions, flexibility and poor modification, this is the fish and bear cake can not be combined. We learn Django, in fact, is to learn a software, to understand its basic principles, grasp its overall framework, keep in mind some basic rules, the rest is to keep in-depth details, and then make perfect, experience how much of the problem, there is no more advanced technology can not be mastered.

Common Commands :

1 #Create a new Django project2django-admin.py Startproject project-name3 #new Django App command4django-admin.py Startapp App-name (or) Python manage.py Startapp app-name5 #Synchronizing Databases6 python manage.py syncdb7 #(django1.7.1 and above need to use the following command)8 python manage.py makemigrations9 python manage.py MigrateTen #run the Django project without port default to 8000 One python manage.py runserver Port A #Ex:python manage.py runserver 8080 - #emptying the database - python manage.py flush the #Create a Super Administrator - python manage.py createsuperuser - #Modify Administrator Password - python manage.py changepassword username + #Exporting Data -Python manage.py dumpdata appname >Appname.json + #Import Data A python manage.py loaddata Appname.json at #go to the Django Project command terminal - python manage.py Shell - #Enter database command line -Python manage.py Dbshell
View Code

Statement:

Combined with the study to make a note, the content of this article more

Http://www.cnblogs.com/feixuelove1009/p/5823135.html

Http://code.ziqiangxuetang.com/django/django-tutorial.html

Http://www.cnblogs.com/z1987/p/5490033.html

Graphical python "12th": Django Basics

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.