cool hit counter How to build a Django application framework in Python? I did!_Intefrankly

How to build a Django application framework in Python? I did!

Django Project

I. Creation of projects

launchpycharm -> create New project-> optionDjango -> (located) atLocation Fill in the project name and path thencreate Just create it.

II. Introduction to Django architecture

Inside HelloDjango, which has the same name as the project, is the configuration file main configuration file url routing file network communication interface

The templates directory holds the html files, also known as T in the MTV architecture Django admin master

III. Running web services

Click on the green triangular arrow to start running the web service, or of course you can use the command line

cd project path

python runserver

Django hands-on

I. Create App

You can include multiple APPs in each django project, and you can think of APPs as subsystems, submodules, functional components, etc. in a large project that are relatively independent of each other, but also connected. All apps share project resources, and there is a terminal terminal entry at the bottom of the project, enter the following command to create an app module called myapp.

python startapp myapp

II. Writing routes

Earlier we had an introduction to Django's architecture, which has an, and the routes are all in the urls file, which maps the url entered by the browser to the appropriate business processing logic.

Import the views file of the corresponding app, here is to import the corresponding views in myapp

Writing routing rules

comment out

# url(r'^admin/', include(,

III. Writing business logic

In the last step we have referenced inside myapp, but our business logic is not written yet, we need to write the business logic in

Punching, we start by importing the HttpResponse module to be used

Define an index method, remember the code we used when writing the route in the live diagram-1? views.index, the index is to call this method to return an HttpResponse response to the user, which index (request) in the request parameter is required, the request is used to handle the response request

IV. Running again

Click on the green triangle arrow, then type Visit.

If nothing else, the error above will appear, haha, this is a deliberate mistake by the coder, take a look at the error prompt what, reported 404 error, just can't find the corresponding interface, and then look back at our previous in

url(r'^index/', views.index),

This means that when the URL path is entered as index, the views.index screen is displayed, but we only enter So he won't be able to find the corresponding page, which naturally reports a 404 error


Method 1: can then (do sth)

Method 2: Change the rules in to

url(r'^', views.index),

final result

1、Happy June 1st Childrens Day In the world of blockchain were all kids
2、Japans Finance Minister Taro Aso Blockchain technology needs to be developed with caution
3、CAS releases big data on World Cup fresh economy Daily Fresh and other head platforms take 75 share
4、What the hell is a blockchain anyway Read the comic and youll understand
5、jQuery Zero Basics Getting Started H AJAX

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送