Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Django knowledge points in python development?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what are the knowledge points of Django in python development?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the knowledge points of Django in python development" article.

Django is the web development framework of python, which follows the design pattern of MVC, but is often called MTV (model-template-views) in Django. Model is the data persistence layer, which mainly stores entity mapping, entity relations and some methods of entities. Template is the presentation layer, which is mainly used to display data. Django's view engine can render it to HTML and display it. Views is the business logic layer, which acts as a bridge between model and template in Django. It processes the model and submits data to template. At the same time, it also accepts template requests and parameters, completes the corresponding logic and submits model modification.

1. Views and URL

Views is the business logic layer. In Django, views is usually a views.py module, which is placed in the corresponding package. Views.py contains specific logic functions, and each function corresponds to one or more templates. In order to establish the relationship between the template and the view, there is also a certain routing mechanism, so Django usually has a routing program urls.py in the root directory. Routes are created by patterns and described by regular expressions, which greatly improves the flexibility of the routing mechanism.

For example:

Views.py

Here the request parameter is required, but you can name it anything you want, as long as it conforms to the specification. Request contains the request information for the page. Sender_to_response is in django.shortcuts, so you have to declare form django.shortcuts import sender_to_response before. Request.MATE contains all the request interface information and user information. Shor () sorts list from smallest to largest. The return value means to submit a values variable to the home.html template. Tuples in patterns in urls add a regular guiding rule: remove those whose original address matches'^ $'to home. Of course, this is provided that the views.py file is in the same folder as urls.py, otherwise you will reference the namespace of home. If you want to pass multiple values in url, you can add parentheses to the matching values you want to pass, such as ('^ ([^ /] +) / ([^ /] +) / $', home) to match / some/some/ and some will be passed to the handler function home. The corresponding home should be accepted by adding appropriate parameters.

two。 Template (Template)

Templates are where data is displayed in Django, usually in HTML format. In templates, the processing logic of Django is written in {%}, and the variables to be displayed are written in {{}}. The Django master page can be used as any document, as long as the block to be filled or replaced is declared with {% block name%} {% endblock%}, and only {% extends master name%} is used and the corresponding block is called.

3. Model

Configure the database in the dictionary of database in setting.py. When the configuration is complete, use manage.py startapp to create app and write python code in models to describe the entity mapping. For example:

Models.py

Models is included in django.db and encapsulates a common interface for model classes. CharField () creates varchar data with parameters such as max_length,blank,verbose_name. Represents the maximum length, whether it is empty, and the display name, respectively. Def__unicode__ provides a default display after boxing, and if this function is not set, the object type is displayed by default. Class Meta specifies the default sort fields for the model. At the same time, Django also provides an interface for setting foreign keys. Here, take book as an example

Add the path to the app package in the setting.py configuration file INSTALL_APPS after creation.

Django supports codefirst to synchronize the database with manage.py syncdb. When updating the database, Django is transformed into a sql statement and then executed. You can run manage.py validate to check the model before execution, or you can run manage.py sqlall books. You can directly declare the model object to achieve data insertion save () save objects.filter () lookup, you can call delete () to delete the object, and you can also call the model to delete batch deletion. By the same token, update also calls individual modifications for objects and batch modifications for models.

4. Integrated subframework

There are a variety of additional feature packages in the django.contrib package. So far, I only understand that admin and auth feel very powerful, but the only drawback is that the interface of admin is slightly ugly. Admin is the backstage management platform provided by Django officially. You can manage the app you add that integrates all the common functions, including additions, deletions, modifications and queries. The calling code is also very simple, only need to activate the link of admin in urls.py, the configuration file is in setting.py, you can change it if you need it. If you want to add the management of app, you need to add the following code (take Book as an example):

5. Caching mechanism

Personally, caching is very important for a website with too many visitors. The caching methods provided in Django are roughly divided into three types: site-wide cache configuration, view cache configuration and data cache configuration. Just modify the relevant configuration file. You can also install other plug-ins to assist with caching, such as memcached.

The above is about the content of this article on "what are the Django knowledge points in python development?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report