Django development basics ---- create a project/application, django ----

Source: Internet
Author: User
Tags install django pip install django

Django development basics ---- create a project/application, django ----

 Environment:

1. python 3.6.2

2. install django: pip install django = 1.10.3

 

* The following describes how to use Django by developing a simple user sign-in system.

 

Create a Django project:

To create a django project using the command line, follow these steps:

A django-admin startproject guest (project name)

B. Go to the guest directory.

C python3 manage. py startapp sign (create an APP. Multiple apps will be generated after multiple executions)

Use PyCharm to open the guest directory. The directory structure is as follows:

Directory description:

Guest:Project container.

Manage. py:A practical command line tool that allows you to interact with this Django project in various ways.

Guest/_ Init _. py:An empty file tells Python that the directory is a Python package.

Guest/Settings. py:Global configuration files, including Django module application configuration, database configuration, and template configuration.

Guest/Urls. py:Route configuration file.

Guest/Wsgi. py:A wsgi-compatible Web server portal to run your project.

 

Migrations/: Used to record data changes in models.

Admin. py: Map data in models to the admin background of Django.

Apps. py: Added in the new Django version for application configuration.

Models. py: Create an application data table model (related database operations ).

Tests. py: Create a Django test.

Views. py: Controls the data displayed to the front end.

 

Next, enter the guest directory and enter the following command to start the server:

Python3 manage. py runserver

By default, Django starts the project through port 8000 of the Local Machine. If this port number is occupied in your current environment, you can also specify the IP address and port number at startup.

Python3 manage. py runserver 127.0.0.1: 8001

Open your browser and access: http: // 127.0.0.1: 8001

Seeing the above page shows that Django has started to work.

The environment is ready. The following describes the real project application development process.

1,Configure the guest/settings. py file and add the sign application to the project.

2. Open the guest/urls. py file to add the route to the user logon page.

3. Open the./sign/views. py file to create the index function.

Define the index function and return the "Hello django" string to the page through the HttpResponse class ". The HttpResponse class exists in django. http. HttpResponse and is passed to the front-end page as a string.

4. Start the django execution Program

A And pycharm can be started directly.

B. Execute the command line: python manage. py runserver 127.0.0.1: 8001

Access 127.0.0.1: 8001/index

5. Use Templates)

Now, if you use an HTML page to replace the "Hello django" string, the processing method will be different.

Create a templates/index.html file under the application sign/directory.

Modify the View File views. py. Here, we discard the HttpResponse class and use the render function of Django. The first parameter of the function is the request object, and the second parameter returns an index.html page.

Access 127.0.0.1: 8001/index again, as shown in figure

To sum up, the Django workflow can be summarized:

It should be noted that this processing process is not a complete process of Django, the most important of which is the absence of data layer (model) operations.

I understandViews FileIt is very important in Django. It is the intermediate link between pages and data. In the example of logon, the user enters the user name and password on the page and clicks to log on. The request will be sent from the view layer.ViewsTo receive, how to extract the user name and password data, how to use the data to query the database, and how to return the login success page to the user, all of which are in the view layerViews.

 

Like most Web development frameworks, Django development also usesMVC Mode.

M: The data access part, which is processed by the Django database layer. This chapter describes the content.

V. The view and template process the data to be displayed and the data to be displayed.

C. According to the user input, the Django Framework calls the appropriate Python function for the given URL Based on the URLconf settings.

 

Since C is processed by the framework itself, Django is more concerned with Model, Template, and Views. Django is also calledMTV framework.

In the MTV Development Mode:

M represents the Model, that is, the data access layer. This layer processes all data-related transactions: how to access and Verify validity

T indicates the Template, that is, the presentation layer. This layer processes performance-related decisions: how to display on pages or other types of documents.

V represents the View, that is, the business logic layer. This layer contains the access model and related logic for obtaining the appropriate template. You can see it as a bridge between the model and the template.

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.