Basic steps to build a Django project on Linux

Source: Internet
Author: User

A Linux Django Basic project setup process:
M model for interacting with the database
V View Reception Request call model get result, call T get page, return to foreground
T template accepts the view request to generate a Web page to V

1 Create a Project command:
Django-admin Startproject Project Name

2 Create an app for your project (one application in Django is a functional module):
There is a manage.py file under this directory under the project directory
Python manage.py startapp app name

3 Installing the app, associating the app with the project
Add an App directory name (app name) in the Installed_apps list in settings.py under project directory

4 Turn on the server:
Django provides a lightweight server written by Python, and the terminal adjusts to the directory where the project resides:
Python manage.py runserver
After that, you can access 127.0.0.1:8000 to access our own servers, as prompted.

Two configuration background receive foreground request
1 The front desk sends the requested entry:
The background accept URL will be in the project directory urls.py file in the regular match, looking for this resource to locate which application
For example, the front desk came http://127.0.0.1:8000/main/index.html
Will go to the front section, leaving Main/index.html/to the urls.py in the project directory for regular matching
Will match to the portion of the deletion after the remainder of the search matches the specified application URLs directory to go
Add an entry in the urls.py in the project directory Urlpatterns:
URL (r ' regular expression ', include ("app directory name. URLs"))
This means that the regular expression match succeeds in removing the matching part to find the urls.py in the application and continues to look for

For example add URL (r ' ^main/$ ', include (' App.urls '))
Http://127.0.0.1:8000/main/index.html, there'll be main/index.html matches left.
There are many items in the URL match to this success, there will be left to remove the matching part of the index.html/to the app this app urls.py look for matching

2 applications in the request directory
After being matched by an item in the 1 process, the remainder is sent to the specified application urls.py file to continue matching
This time index.html/will enter the app's urls.py.
We need to add a URL ("regular", "package. function") to the application's urls.py.

such as URL (r "^index.html/$", Views.index)
This means: The successful match request will go to views.py to find the index function,
This index function is then written next,

3 Response requested in application
Main/index.html/in the project urls.py to an app,
A function is matched to the urls.py that enters the application View.index
We need to write the index function in the view.py in the application directory to accept the request and return the response
The function must receive the parameter request
Note that the return is HttpResponse (the string of the page)
such as Def Index (request):
return HttpResponse ("hahaha")

Here is the views inside, this MVT view.
The foreground sends the request after a match, and here, receives the request.
Here on request,
May go to the database to check some data: is called models.py inside the method and the database interaction
Call the method inside T: template requires our own creation and registration,
It mainly provides some HTML pages,
We put the result of calling models into the HTML stitching
Finally, wrap the returned page back in a good return

4 in Browser input http://127.0.0.1:8000/main/index.html
will be accepted in the background. Based on the return value of the function called below the last views, get a different page
According to my example will get hahaha


Here, a Django project is completed, the foreground send request can be successfully accepted in the background and return the response

Basic steps to build a Django project on Linux

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.