[Python] Getting Started with the Django framework 5--static files, middleware, uploading pictures and paging

Source: Internet
Author: User

Description

This article mainly describes other Django content, involving static file processing, middleware, uploading files, paging, and so on.

Development environment: WIN10, Python3.5, Django1.10.

  

One, static file processing

Static files in a Django project refer to files such as CSS, JS, and pictures.

1. Configure static files

To configure static content in the settings.py file:

    static_url: The path used in the HTML file, such as: "/static/images/logo.png".

    Staticfiles_dirs: Refers to the location where the static files are stored in the project.

    

    

2. New Directory

Create a static directory under the project directory (sibling to manager.py), and then create the name of the current app under the static directory:

at this point, you can add static files to the corresponding application: "/static/myapp/css", "/static/myapp/js" .

3. Application

You can use hard-coded in templates

    

Static encoding can be used in templates

Second, the middleware

The middleware in Django is a lightweight, low-level plug-in system that can intervene in Django's request and response processes to modify Django input or output.

Use middleware: Add middleware to the middleware_classes tuple in the configuration file (settings.py).

1. Each middleware component is a separate Python class that can define one or more of the following methods:

    __init__: No parameters are required, the server is called once in response to the first request and is used to determine whether the current middleware is enabled.

    process_request (request): Called before execution of the view, called on each request, returns None or HttpResponse object.

    Process_view (Request, View_func, View_args, View_kwargs): Called before the view is invoked, called on each request, returns none or the HttpResponse object.

    Process_template_response (Request, response): Called after the view has just finished executing, called on each request, and returns the response object that implements the Render method.

    Process_response (Request, Response): All responses are called before being put back into the browser, called on each request, and the HttpResponse object is returned.

    process_exception (Request, response, exception): Called when the view throws an exception, called on each request, returns a HttpResponse object.

  

    

    

  

2. Example: Custom exception Handling

Create the myexception.py file in the sibling directory with the configuration file (settings.py), define the class MyException, implement the process_exception method, and encode the following:

    

    

"Register middleware, register myexception to settings.py Middleware (middleware, django1.4-1.9 is middleware_classes):

    

"Defines a view and throws an exception, the custom exception handling is run

    

    

Third, upload files

When Django is processing a file upload, the file data is stored in the request. Files.

Each key in files is a name in <input type= "file" Name= "" >.

It is important to note that files only contain data if the request is in the form of post and the <form> with enctype= "Multipart/form-data" is submitted. Otherwise, files will be an empty dictionary-like object.

Here is an example (save the uploaded image):

1. Use the model to process uploaded files: Define the attribute as models. ImageField type

    

Note: If the property type is ImageField, you need to install Pilow, and the installation ignores

    

2. Picture Storage path:

The media folder is created under the static directory of the project and is saved to the "/static/media/picture file" After the image is uploaded.

Open the settings.py file, and add the Media_root entry:

    

You can use the Django Admin background, a ImageField type attribute will appear with a files box to complete the upload.

    

3. Manually upload the template code:

    

    

4. Manually upload the View code:

    

  

Four, pagination

The Django Framework provides a number of classes that implement management data paging, which are located in django/core/paginator.py.

1.Paginator Object

    __init__ (list, int): Returns the paging object, the parameter is the list data, the number of bars per page of data.

    Count: Total number of objects.

    num_page: Total number of pages.

    page_range: List of pages, starting from 1, for example [1, 2, 3, 4]

    page (num): The subscript starts with 1 and throws a Invalidpage exception if the provided page number does not exist.

Exception exception:

Invalidpage: Thrown when an invalid page number is passed to page ().

Pagenotaninteger: Thrown when a value that is not an integer is passed to the page ().

Emptypage: When a valid value is provided to page (), but no object is thrown on that page.

  

2.Page Object :

The page () method of the Paginator object returns the Page object and does not need to be built manually.

Property:

      object_list: A list of all objects on the current page.

      Number: The ordinal of the current page, starting at 1.

      paginator: The Paginator object associated with the current Page object.

Method:

      Has_next (): Returns True if there is a next page.

      Has_previus (): Returns True if there is a previous page.

      has_other_pages (): Returns True if there is a previous or next page.

      Next_page_number (): Returns the page number of the next page and throws a Invalidpage exception if the next page does not exist.

      Len (): Returns the number of current page objects.

Iterate Page objects: Returns each object in the current page

3. Example

"Create a View

"Configure URL

    

"Define Templates

[Python] Getting Started with the Django framework 5--static files, middleware, uploading pictures and paging

Related Article

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.