From zero to Django Daniel's Advanced Path 01

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

Build a virtual environment
    • Mkvirtualenv django_py3_1.11-p Python3
Installing Django
    • Pip Install django==1.11.11
Create a project
    • Django-admin Startproject Project Name
    • Project Catalogue
      • Settings.py is the overall configuration file for the project.

urls.py is the URL configuration file for the project.

Wsgi.py is a project-compatible Web server portal with Wsgi.

Manage.py is a project management document that manages projects through it.

Run the development server
    • Python manage.py runserver IP: Port
Create sub-apps
    • Python manage.py startapp Child app Name
    • Sub-app Directory
      • The admin.py file is associated with the site's background management site configuration.

The apps.py file is used to configure information about the current sub-application.

The migrations directory is used to store the database migration history file.

The models.py file user saves the database model class.

The tests.py file is used to develop test cases and write unit tests.

views.py files for writing web app views

    • Registering the installation sub-application
      • Add the config class from the configuration information file apps.py of the sub-app to the Installed_apps list
Create a View

From django.http import HttpResponse
def index (Request):

"""index视图:param request: 包含了请求信息的请求对象:return: 响应对象"""return HttpResponse("hello the world!")
Defining Route URLs
    • Create a new urls.py file in your child app to hold the route for that app.
    • Adds the routing data for the child app in the project total route demo/urls.py.
Configuration, static files, and routing profiles
    • Base_dir
      • Root directory of the current project
    • DEBUG
      • Debug mode, the initial value is true after the project is created
    • Local language and time zone
static files
    • Staticfiles_dirs storing directories to find static files
    • Static_url URL prefixes for accessing static files
Route description
    • The corresponding routing rules are found from the Urlpatterns list in the main route file in order from top to bottom, and if the discovery rule is include, then the Urlpatterns list in the included URLs is queried from top to bottom.
Reverse Inverse analysis
    • When defining a route using the Include function, you can use the namespace parameter to define the namespace of the route
    • When defining a normal route, you can use the name parameter to indicate the name of the route
    • Using the reverse function, you can return a specific path based on the route name
Request Response Request
    • URL path parameters
      • You can use the regular expression to extract parameters from the URL to get the request parameters
        • Unnamed parameters are passed in defined order
        • Named parameters are passed by name
    • Querydict Object
      • HttpRequest object's properties Get, post are objects of type querydict
      • Method Get (): Gets the value based on the key
      • Method GetList (): Gets the value from the key, the value is returned as a list, you can get all the values of the specified key
    • Querying string query strings
      • Request. Get property gets
    • Form Type Form Data
      • can be obtained by request. The Post property gets, returning the Querydict object.
    • Non-form type Non-form Data
      • The most primitive request body data can be obtained through the Request.body property.
Response
    • HttpResponse
      • HttpResponse (content= response body, content_type= response body data type, status= status code)
    • Jsonresponse
      • To return JSON data, you can use Jsonresponse to construct the response object
    • REDIRECT Redirection

From zero to Django Daniel's Advanced Path 01

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.