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

settings.py main configuration file

urls.py url routing file

wsgi.py network communication interface

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

manage.py 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 manage.py 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 manage.py startapp myapp

II. Writing routes

Earlier we had an introduction to Django's architecture, which has an urls.py, 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(admin.site.urls)),

III. Writing business logic

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

Punching views.py, 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 http://127.0.0.1:8000/ 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 urls.py

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 http://127.0.0.1:8000/ So he won't be able to find the corresponding page, which naturally reports a 404 error

cure

Method 1: http://127.0.0.1:8000/index can then (do sth)

Method 2: Change the rules in urls.py to

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

final result


Recommended>>
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字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号