Django - bernardopnunes/SoftwareEngineeringSDUW GitHub Wiki
- Wiki Master
- right light team
- California
- The Beauties
- The Hateful Eight
- Seven Dwarves
- What is Django
- Design of Django
- Design philosophy
- Advantages of Django
- Shortcoming
- The development trend of Django
- History
- Which Django version is better
- "Hello World" in Django
- Django forms
- Reference
Django is an open-source framework for Web development. And the underlying code was written in Python. In 2005 July, Django was published in the permission of Berkley Software Distribution (BSD). Also, Django is aiming to develop a database-driven website in a simple, sharp, and agile way. What is important to Django is code-reuse. Many components can serve the whole frame with the form of plug. At the same time, Django has tons of powerful third-party plugs you can use.
For now, Django has become the web developer's first choice. It's universally acknowledged that the MVC model is a noble model in web design, and Django adopts the MTV design model, which is similar to the MVC model.
MVC represents the model, view, and controller which divided web application into 3 parts. And they connect to each other with the form of plug.
layer | function |
---|---|
Model | Dealing with data. exp: How to access data |
view | Dealing with the appearance |
Controller | Dealing with logic between model and template |
Django's MVC based design is beautiful:
1.ORM (object relational mapping): define your data model in the form of Python class. ORM connects the model and relational database, and you will get a very easy to use database API. At the same time, you can also use the original SQL sentences in Django.
2.URL dispatch: using regular expressions to match URLs, you can design arbitrary URLs without framework specific restrictions. As flexible as you like.
3.Template system: using Django's powerful and extensible template language, you can separate design, content and Python code. And it is inheritable.
4.Form processing: you can easily generate a variety of form models to achieve the validity of forms. You can easily generate the corresponding form from the model instance you defined.
5.Cache system: can be hung in memory buffer or other framework to achieve super buffer - to achieve the granularity you need.
6.Session, user login and permission check, rapid development of user session function.
7.Internationalization: built in internationalization system, convenient to develop websites in multiple languages.
8.Automated management interface: you don't need to spend a lot of work creating people to manage and update content. Django comes with an admin site, similar to the content management system.
MTV model is similar to the MVC model. However, there are some differences in the definition.
layer | function |
---|---|
Model | Dealing with database and objecte |
Template | Showing the pages to user |
Controller | Dealing with logic between model and template |
- Stands for Model, the data access layer. This layer handles all transactions related to the data: how to access it, how to validate it, what behaviors are included, and the relationships between the data.
- Stands for Template, the presentation layer. This layer handles performance-related decisions about how to display in a page or other type of document.
- This layer contains the logic to access the model and retrieve the appropriate template. You can think of it as a bridge between the model and the template.
Django pursues low coupling and high cohesion of each subsystem (layer). Keep the code independent, function independent and no cross-linking between layers. For example, the template layer does not need to know the specific situation of the user's web request, the model layer does not need to know how the template layer displays the data, and the view layer does not care about which and how the programmer uses the template system.
Django suggests that the code of each app should be as simple as possible and make full use of Python's dynamic capabilities, such as introspection.
Django was born in a news editing agency. Its application environment requires rapid development and iteration, so at the beginning of the design, it pursues to achieve the processing of requirements at a faster speed. You only need to write some new code or modify some local code to achieve a new site.
Unless there are special requirements, all the libraries, tools, plug-ins, and functions that have been provided by the official or ecosystem, please use them directly instead of developing them by yourself.
The basic meaning of this principle is: don't play tricks and show off skills, use more common, clear, and intuitive grammar as much as possible, and don't use obscure grammar. Write your code more verbose, more straightforward, more clear, more than two lines of fear, more comments better.
The framework should be consistent at all levels. Consistency applies to everything from low-level (Python's coding style) to high-level (using Django's "experience"). This rule has both code specification requirements and development habits requirements and should maintain a unified style throughout the project.
Fields should not assume certain behavior based on the name of the field alone. This requires too much knowledge of the system and is prone to errors. Instead, its behavior should be based on key parameters and, in some cases, on the type of field. In other words, instead of specifying the function of a field by its name, you should design the field by selecting its type in detail and defining its parameters.
The model should encapsulate all aspects of an "object" and follow Martin Fowler's active record design pattern. That is to say, for a model, any meta information, method, function, attribute related to it, including its human-readable name, default sorting and other options, all the information needed to understand the model should be stored in the model, rather than being implemented in the view, URL or template.
SQL statements should be executed as few as possible and should be optimized internally. Developers need to call save () explicitly instead of the framework silently saving the data behind the scenes.
The ORM API should allow as few syntaxes as possible to express rich and meaningful statements. It should not depend on importing other modules or helper objects. Every object should be able to access all related objects and system scope, and this access should be bidirectional.
The API of ORM is just a convenient method, but not all the final means. The framework must support the use of native SQL statements, which Django has achieved.
URLs in Django applications should not be coupled with underlying Python code. Linking URLs to Python function names is a bad and ugly practice.
URLs should be as flexible as possible. Any conceivable URL design should be allowed.
Design beautiful URLs, not ugly ones. File suffixes should be avoided in URLs. Vignette like commas should not be used in URLs.
Technically, 123.com/bar and 123.com/bar/ are two different URLs that search engine crawlers see as separate pages. But Django will turn it into a "standard" URL for search engine crawlers to recognize correctly.
We see the template system as a tool for controlling the logic associated with presentation and presentation. The template system should not support functions beyond this basic goal.
Most dynamic websites will use the general design of some websites, such as the general header, footer, navigation bar, and so on. The Django template system follows this, making it easy to store these elements in one place, thereby reducing duplicate code.
The template system should not be designed to output only HTML. It should also be good at generating other text-based formats, or just plain text.
Using the XML engine to parse the template will introduce many human errors in the process of editing the template, and lead to unacceptable overhead in template processing.
Django expects template writers to be able to edit HTML text directly.
Template systems should not do magic things with whitespace. If the template contains whitespace, the system should process the whitespace as it processes the text - just display it. Any whitespace that is not in the template label should be displayed.
The goal of the template system is not to invent a programming language. Its goal is to provide enough programming style functions, such as branching and looping, which are essential for making performance-related decisions. The Django template language (DTL) is designed to avoid high-level logic. The Django template system believes that templates are usually written by designers, not programmers, so it should not be assumed that he understands python. Therefore, when we use Django's template system, we will find that this is only a rendering tool with general programming functions. Do not try to use it as a powerful and syntactically complete programming language.
The out of the box template system is forbidden to contain malicious code, such as code for deleting database records. This is another reason the template system does not allow arbitrary Python code.
The template system should recognize that high-level template authors may want to extend it. This is the concept behind custom template tags and filters.
-
When starting Django server with manage. Py runserver, settings. Py in the same directory will be loaded. This file contains configuration information in the project, such as urlconf, etc. the most important configuration is root_ Urlconf, which tells Django which Python module should be used as the urlconf of the site. The default is URLs. PY
-
When accessing the URL, Django will follow the root_ The setting of urlconf to load urlconf.
-
Then match the urlpatterns in urlconf one by one. If found, the associated view function is called with the httprequest object as the first parameter (usually request)
-
Finally, the view function returns an httpresponse object
Writing views should be as simple as writing Python functions. Developers should not instantiate a class at function execution time.
The view should have access to a request object - an object that stores metadata about the current request. Objects should be passed directly to the view function, rather than the view function that must access the requested data from a global variable. This makes it easy, clean, and easy to test the view by passing in a "fake" request object.
The view should not care about which template the developer uses - not even the templating system at all.
Get and post are different; developers should explicitly use one or the other. The framework should make it easy to distinguish get and post data.
Caching should be as fast as possible. Therefore, all framework code around the cache backend should be kept to an absolute minimum, especially for get () operations.
The cache API should provide a consistent interface for different cache back ends.
The caching API should be scalable at the application level based on the needs of developers.
With Django, you can take Web applications from concept to launch in a matter of hours. Django takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Django was designed to help developers take applications from concept to completion as quickly as possible.
Django includes dozens of extras you can use to handle common Web development tasks. Django takes care of user authentication, content administration, site maps, RSS feeds, and many more tasks — right out of the box.
Django takes security seriously and helps developers avoid many common security mistakes, such as SQL injection, cross-site scripting, cross-site request forgery and clickjacking. Its user authentication system provides a secure way to manage user accounts and passwords.
Some of the busiest sites on the planet use Django’s ability to quickly and flexibly scale to meet the heaviest traffic demands.
Companies, organizations and governments have used Django to build all sorts of things — from content management systems to social networks to scientific computing platforms.
- Over-encapsulation: many classes and methods are encapsulated, which makes it easier to use directly, but harder to change.
- Compared to C and C ++, Django's performance is low
- This is all for you :Django comes with some lightweight applications that don't need it, and it's not portable like Flask.
Let's discuss the development trend of Django in the next few years.
Django is mainly for Web development services, so the future of Django is closely related to the future development trend of Web development. Django, a traditional framework based on the MVC development model, is very suitable for the development of traditional PC-based websites, because it includes both back-end development and front-end development. PC-based websites and adaptive mobile phone websites will not disappear,however,their importance will gradually decrease with the popularity of mobile apps and applets.At the front end, you still need to rely on some popular JS frameworks such as Vue.js to develop excellent interactive websites that meet future aesthetic trends.
Besides,the more popular development model in recent years and the next few years is definitely the separation of front and back ends. The modern web application Web APP or large website is generally a background, and then corresponds to various clients. Since the development language of the client is often different from the development language of the backend, the backend needs to provide a standard resource or data that can be cross-platform and cross-language for front-end and back-end communication. Django itself cannot develop a Web API that conforms to the REST specification, but with the help of django-rest-framework (DRF), a well-specified Web API can be quickly developed.
Python is the soil on which Django lives. Python rises, Django rises, Python declines, Django declines. At present, Python is still very hot in various fields such as Web development, scientific computing, and artificial intelligence. From this point of view, Django should not have any crisis in the next few years.
The development of Django cannot be driven purely by web and python,Blacksmith need its own hardware.It has powerful functions and high development efficiency and security to win users' love. The Django English documentation is still very good, and the original team is also actively maintaining and constantly updating the version. At present, the user community of Django is growing.
Django was created in the fall of 2003, when the web programmers at the Lawrence Journal-World newspaper, Adrian Holovaty and Simon Willison, began using Python to build applications.It was released publicly under a BSD license in July 2005. The framework was named after guitarist Django Reinhardt. In June 2008, it was announced that a newly formed Django Software Foundation (DSF) would maintain Django in the future. Django was released on 21, July 2005. Its current stable version is 2.2.5 which was released April 1, 2019.
This app supports the following combinations of Django and Python:
Django | Python |
---|---|
1.11 | 2.7,3.4,3.5,3.6,3.7 |
2.0 | 3.4,3.5,3.6,3.7 |
2.1 | 3.5,3.6,3.7 |
2.2 | 3.5,3.6,3.7 |
3.0 | 3.6,3.7,3.8 |
We improve Django almost every day and are pretty good about keeping the code stable. Thus, using the latest development code is a safe and easy way to get access to new features as they’re added. If you choose to follow the development version, keep in mind that there will occasionally be backwards-incompatible changes. You’ll want to pay close attention to the commits by watching Django on GitHub or subscribing to django-updates.
If you’re just looking for a stable deployment target and don’t mind waiting for the next release, you’ll want to stick with the latest official release (which will always include detailed notes on any changes you’ll need to make while upgrading).
"Hello World" is the beginning of any language, so our learning of Django also starts here. As mentioned earlier in another group, Django is MVT structure, because no data and templates are used here, only coding in V (view layer) is needed.Open view.py and start coding:
def hello(request):
return HttpResponse('Hello World')
After the view layer is written, it will eventually respond to 'Hello World' through HttpResponse. As mentioned earlier, urls are used to declare the mapping relationship of the requested url. That is, the program finds the view we wrote through the configuration in urls.
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^hello/$', views.hello)
]
Start the project by executing the following command:
python3 manage.py runserver
The default port number is: 8000, when the 8000 port is occupied, we can also manually replace the port, such as 8080.
python3 manage.py runserver 8080
After the project starts successfully, enter the following in the browser,we can see "Hello World" in the page.
http://localhost:8000/hello/
HTML forms are a classic way of website interactivity. I will introduce how to use Django to process the form data submitted by users.
The HTTP protocol works in a "request-reply" way. When a customer sends a request, they can attach data to the request. By parsing the request, the server can obtain the data from the client and provide specific services based on the URL.
We created a search.py file in the previous project to receive user requests: /HelloWorld/HelloWorld/search.py:
# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.shortcuts import render_to_response
# form
def search_form(request):
return render_to_response('search_form.html')
# Receive request data
def search(request):
request.encoding='utf-8'
if 'q' in request.GET and request.GET['q']:
message = ' search_info: ' + request.GET['q']
else:
message = 'Empty form'
return HttpResponse(message)
Add the search_form.html form in the template directory templates: /HelloWorld/templates/search_form.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> </title>
</head>
<body>
<form action="/search" method="get">
<input type="text" name="q">
<input type="submit" value="搜索">
</form>
</body>
</html>
The urls.py rule is modified to the following form: /HelloWorld/HelloWorld/urls.py:
from django.conf.urls
import url from . import view,testdb,search
urlpatterns = [
url(r'^hello$', view.hello),
url(r'^testdb$', testdb.testdb),
url(r'^search-form$', search.search_form),
url(r'^search$', search.search),
]
Above we used the GET method. View display and request processing are divided into two function processing. The POST method is more commonly used when submitting data. We use this method below, and use a URL and processing function to display the view and process the request at the same time. We create post.html in templates: /HelloWorld/templates/post.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> </title>
</head>
<body>
<form action="/search-post" method="post">
{% c %}
<input type="text" name="q">
<input type="submit" value="Submit">
</form>
<p>{{ r }}</p>
</body>
</html>
At the end of the template, we add a ‘r’ mark to reserve a place for the table processing results. There is also a {% c %} label behind the table (Cross Site Request Forgery). This is a feature provided by Django to prevent submission requests from being disguised. Forms submitted by the POST method must have this label. Create a new search2.py file in the HelloWorld directory and use the search_post function to process POST requests: /HelloWorld/HelloWorld/search2.py:
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.views.decorators import csrf
# Receive POST request data
def search_post(request):
ctx ={}
if request.POST:
ctx['r'] = request.POST['q']
return render(request, "post.html", ctx)
The urls.py rule is modified to the following form: /HelloWorld/HelloWorld/urls.py:
from django.conf.urls import url
from . import view,testdb,search,search2
urlpatterns = [
url(r'^hello$', view.hello),
url(r'^testdb$', testdb.testdb),
url(r'^search-form$', search.search_form),
url(r'^search$', search.search),
url(r'^search-post$', search2.search_post),
]
Django 1.0 has established a powerful and complete document system, covering all aspects of Django, and is suitable for readers and developers at all levels. It also includes several simple examples, with which you can follow them step by step to experience the beauty of Django.
Django version mainly consists of the following parts:
First steps, which provides a quick start tutorial, can start using Django soon
The model layer, which introduces Django's abstract model layer
The view layer introduces Django's view layer
The template layer, which introduces Django's template layer
Forms, a series of tools provided by Django to help use forms
The development process introduces a series of tools provided by Django for development and testing
The admin introduces the tools provided by Django for site and content management
Security, a tool provided by Django for site security control
Internationalization and localization, introducing the tools provided by Django for globalization and localization
Performance and optimization, introducing the tools and suggestions provided by Django for performance and optimization
Python compatibility, which introduces Django's compatibility in different Python environments (Jython and python 3)
Geographic Framework, introducing the location related tools provided by Django
Common web application tools, introduce some common web application tools provided by Django (such as RSS, etc.)
Other core functionalities, introduce other important tools provided by Django (such as jump, routing, etc.)
The Django open source project introduces some contents of Django as an open source project, such as design philosophy, how to participate in development, etc.
Django is capable of developing large and medium-sized websites or the mobile app backstage,so the Django's development should still be on the rise in the next few years by virtue of its high efficiency, safety and current popularity.
The following content is writtern by Group California.
Django is an open-source Web development framework written in Python language and designed according to MVC. It was written by a network development team in Lawrence City, Kansas State. It was born in the fall of 2003, when Adrian holovaty and Simon Willison, programmers of the Lawrence journal world newspaper, began programming in Python.
At that time, their world online team produced and maintained several local news sites, and gradually developed in a fast-paced development environment unique to the press.
These sites include ljworld.com, lawrence.com and kusports.com. The management requires that the whole program can be established quickly within the planned time. Therefore, Adrian and Simon have developed a time-saving network program development framework, which is the only way to complete the program before the deadline.
In the summer of 2005, when the framework was developed, it had been used to build many world online sites. At that time, Jacob Kaplan moss in the world online group decided to release the framework as an open source software.
In the next few years, Django is a perfect open source project with tens of thousands of users and contributors and widely spread in the world.
The original world online developers (Adrian and Jacob) still master Django, but the development direction of Django is more affected by the community team.
Django was born in the environment of news websites, so it provides many features (such as management background), which are very suitable for content websites, such as amazon.com, craigslist.org and washingtonpost.com, which provide dynamic, database driven information.
(although Django is good at dynamic content management system, it doesn't mean that Django's main purpose is to create websites for dynamic content. There is a difference between being particularly efficient in some areas and being inefficient in others, and Django is equally efficient in others.)
Django's origins have shaped the culture of its open source community. Because Django comes from real world code, not from a scientific research project or commercial product, it mainly focuses on solving problems encountered in web development and problems often encountered by Django developers.
In this way, Django makes progress every day on the basis of the existing. The developers of the framework are very interested in making developers save time, write programs that are easier to maintain, and ensure the efficiency of program operation.
Developers are motivated by their goals: save time and work happily.
Django mainly serves for Web development, so Django's future is closely related to the future development trend of Web development. Django, a traditional framework based on MVC development pattern, is very suitable for developing traditional websites based on PC, because it includes both back-end development (logic layer, database layer) and front-end development (such as template language, style). PC-based websites and responsive mobile websites will not disAPPear, but their importance will gradually decrease with the popularity of mobile app and small programs. Even for the development of traditional websites, Django has some advantages only in the background development. In the front end, it is still necessary to use popular JS frameworks such as Vue.js to develop excellent websites with strong interactivity and in line with future aesthetic trends.
The Django framework was originally mainly used to develop websites, but with the development of mobile Internet, Python and Django are increasingly used in the background development languages of various mobile APP. There is usually only one background for modern Web APPlication web app, and then the clients are various (iOS, android, browser), and the development language of the clients is probably different from that of the background. At this time, we need the background to provide a standard data exchange format (JSON or XML) that can cross platforms and languages for front-end and back-end communication. This is the Web API (Network Interface). With the maturity of django-rest-framework, it becomes very easy to use Django as the background to provide REST-compliant Web API(json format data) to interact with different clients (iOS, android). The more popular development mode in recent years and the next few years is definitely separation of front and back ends. Modern Web APPlications such as web app or large websites are usually a backstage and then correspond to various clients (iOS, android, browser). Since the development language of the client is often different from that of the background, the background is required to provide a standard resource or data (such as Json format) that can cross platforms and languages for communication between the front and back ends. This is the role of Web API (Web Application Interface). Django itself cannot develop a Web API conforming to REST specifications, but with the help of django-rest-framework (DRF), it can quickly develop a Web API of excellent specifications. Therefore, we would like to thank DRF here, because it has a wider application prospect for Django and reduces the risk of being eliminated. Python is the soil Django lives on. At present, Python is still very hot in various fields of Web development, scientific computing and artificial intelligence. From this point of view, Django should have no crisis in the next five years.
Due to Django's unique advantages, there are many popular websites built using Python on the Django framework. The following are some major sites built entirely or partially on Django.
It is one of the world's most popular hosting websites for blog comments. Disqus allows easy integration with most popular CMS (content management systems), such as WordPress. Django has more than 50 million users and can meet the needs of site owners to contact the community.
Onion website provides an online place for their satirical newspaper and Django provides a framework for it.
Bitbucket is like GitHub, a version control repository hosting service. The only difference between Bitbucket and GitHub is that Bitbucket hosts the Mercurial repository while GitHub hosts the git repository. As millions of users are associated with BITBUCK, all services provided by BITBUCK (such as creating repositories, pushing code, adding collaborators, submitting, pulling requests, etc.) must be stable. Django runs the bitbucket website.
Instagram is a social networking application specially created for those who like to share photos and videos with all their friends. At present, Instagram has many celebrities to keep in touch with fans. The Django framework is also running Instagram.
Mozilla browser is the second most widely used browser in the world after Google Chrome. Now Mozilla's help page is built using the Django framework.
Millions of users around the world have discovered their new ideas and inspiration from Pinterest. Pinterest is using the Django framework (modified according to its requirements) to run it.
NASA's official website is a place where thousands of users visit and view news, images, videos and podcasts provided by premiere agencies. Django has developed specific parts of NASA's official website.
If there is only one influential newspaper in the world, it must be the Washington Post. The Washington Post website is a popular online news source accompanying their daily newspapers. The Django Web framework can easily handle its large number of views and traffic.
The popular Reddit website has launched an online anonymous gift exchange and gathering platform called Reddit Gifts. The website connects users from all over the world and promotes gift exchange between them. Django Web framework enhances its functions.
Prezi is a cloud-based alternative to Microsoft PowerPoint based on Django framework. The site provides a virtual canvas that can be manipulated and zoomed in and out. This provides an entire view of the presentation, not a single slide.
A fundamental goal of Django’s stack is loose coupling and tight cohesion. The various layers of the framework shouldn’t “know” about each other unless absolutely necessary.
For example, the template system knows nothing about Web requests, the database layer knows nothing about data display and the view system doesn’t care which template system a programmer uses.
Although Django comes with a full stack for convenience, the pieces of the stack are independent of another wherever possible.
Django apps should use as little code as possible; they should lack boilerplate. Django should take full advantage of Python’s dynamic capabilities, such as introspection.
The point of a Web framework in the 21st century is to make the tedious aspects of Web development fast. Django should allow for incredibly quick Web development.
Every distinct concept and/or piece of data should live in one, and only one, place. Redundancy is bad. Normalization is good.
The framework, within reason, should deduce as much as possible from as little as possible.
This is a core Python principle listed in PEP 20, and it means Django shouldn’t do too much “magic.” Magic shouldn’t happen unless there’s a really good reason for it. Magic is worth using only if it creates a huge convenience unattainable in other ways, and it isn’t implemented in a way that confuses developers who are trying to learn how to use the feature.
The framework should be consistent at all levels. Consistency applies to everything from low-level (the Python coding style used) to high-level (the “experience” of using Django).
Fields shouldn’t assume certain behaviors based solely on the name of the field. This requires too much knowledge of the system and is prone to errors. Instead, behaviors should be based on keyword arguments and, in some cases, on the type of the field.
Models should encapsulate every aspect of an “object,” following Martin Fowler’s Active Record design pattern.
This is why both the data represented by a model and information about it (its human-readable name, options like default ordering, etc.) are defined in the model class; all the information needed to understand a given model should be stored in the model.
Django follows a MVC- MVT architecture.
MVC stands for Model View Controller. It is used for developing the web publications, where we break the code into various segments. Here we have 3 segments, model view and a controller.
Model -- Model is used for storing and maintaining your data. It is the backend where your database is defined.
Views -- In Django templates, views are in html. View is all about the presentation and it is not at all aware of the backend. Whatever the user is seeing, it is referred to a view.
Controller -- Controller is a business logic which will interact with the model and the view.
The core components of the Django framework are:
- Object relational mapping used to create the model;
ORM (object-relational mapping) : define your data model as a Python class. ORM connects the model to a relational database, and you get a database API that is very easy to use. You can also use raw SQL statements in Django.
- Design a better management interface for end users;
Automated management interface: you don't have to spend a lot of work creating people management and updating content. Django comes with an ADMIN site, similar to a content management system.
- URL design;
URL dispatch: using regular expressions to match urls, you can design arbitrary urls without the framework's specific constraints. Be as flexible as you like.
- Designer friendly template language;
Template system: use Django's powerful and extensible template language to separate design, content, and Python code. And it's inheritable.
- Caching system.
Cache system: can be hung in memory buffer or other framework to achieve super buffer - achieve the granularity you need. Session, user login and permission check, rapid development of user session function.
- Other
Form processing: you can easily generate a variety of form models to achieve form validation. You can easily generate the corresponding form from the model instance you define.
Internationalization: the built-in internationalization system facilitates the development of multi-language websites.
MVT stands for Model View Template. In MVT, there is a predefined template for user interface. With template, you will be having only one file that prints hello along with the variable name. Now this variable will be substituted in that particular template using some jinja logic. That's the magic of template, you don't need to rewrite the code again and again!
In the above image, template is your front end which will interact with the view and the model will be used as a backend. Then view will access both the model and the templates and maps it to a url. After that, Django plays the role of controller and serves it to the user.
In Django, the part of the controller that accepts user input is handled by the framework itself, so Django focuses more on models, templates, and Views, known as the MTV pattern. Their respective responsibilities are as follows:
Hierarchical | responsibility |
---|---|
The Model, or data access layer | handles all transactions related to the data: how it is accessed, how it is validated, what behaviors are included, and the relationships between the data. |
Templates, or presentation layers | handle performance-related decisions about how to display in a page or other type of document. |
View, the logic that allows the business logic layer to access the model and retrieve the appropriate template | Bridge between model and template. |
Along with the rising of Python, Django has become popular too. Thanks to some superficial development experience, I would like to share my superficial views on some common problems in development by Django.
the relative definition of Java can be their own constraints and configuration, can completely according to their own company's structure and habits to write, so there is no such thing as flexibility. Django is also relatively similar, for static directory through the system's OS, path and setting configuration can specify the corresponding directory as a static directory or file upload directory and similar functions. In terms of system function planning, the working directory generated through Django will provide basic programs, including the initial wsgi, setting, url and other basic project files. In addition, Django manager startapp will generate the corresponding app directory structure in the same directory. Generally speaking, the file structure is generally fixed. It is worth mentioning that Python's TAB spacing habit makes the code clear at a glance, which improves the readability of the code.
Django can build projects from scaffolding, build modules, create databases from models, and create database migration tasks.
Django can generate module code by command, while Java can’t. Some common ListView, DetailView, and TemplateView are also encapsulated in the development of Django to reduce the amount of code being developed. After generating the module code, Django can register with admin.py in app via @through the definition of the built-in Model layer to enter the background management of the system. The web interface can manage data without any development. This, I believe, is one of the most powerful and maligned aspects of Django. The advantage is that the model can be injected into the management platform without any code development. Only need to configure the fields of the model to achieve the CURD of the model, List page filter search.
The disadvantage is that the management platform is very rigid and difficult to modify, such as modifying the template style in the background and adding some common processing to template variables (such as replacing {{temp}} from 1 to "male", which requires writing code to achieve). You can't use Python's functionality in a template; you need to conform to the template's Api syntax.
Java's strength is in the system architecture, such as: workflow module, system permission module, data cache, sharding, microservices and other architecture levels. As a result, Django is more approachable for everyday business code development, and it is more advantageous for developing mid-to-low scale systems.
- Baike.baidu.com. 2020. Django(Python Web Frame)_Baidu. [online] Available at: https://baike.baidu.com/item/Django/61531 [Accessed 21 May 2020].
- Wiki.hk.xileso.top. 2020. Django - Wikipedia【 Wikipedia Web】. [online] Available at: https://wiki.hk.xileso.top/wiki/Django [Accessed 21 May 2020].
- Pictures[pictures] Available at: http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=django
- Django 1.11.30.dev20200305075726 documentation:https://django.readthedocs.io/en/1.11.x/ [Accessed 22 May 2020].