Just starting to get in touch with Django today, to be honest, the lazy people I don't even want to learn from Python can take the initiative to learn about Django, which is really the sun playing west. But there's a reason for everything, but if it wasn't for the form, I wouldn't have learned anything about Django's anti-human stuff (as one of my friends said). To tell the truth, I still like Java, killing me also like Java. Now that you've started the Django study, it's like a bit of a good look.
First, we have to understand what Django is.
This is explained on Baidu: Django is an open-source Web application framework, written by Python. The MVC framework pattern is used. Model, view, controller. It was originally designed to be used to manage the Lawrence group's news sites, which are content management system software. and was released in July 2005 under the BSD license. The framework is named after the Belgian gypsy jazz guitarist Django Reinhardt.
The core components are:
1. Object-relational mappings used to create the model (to be honest, I don't know anything, but the model is a more critical thing)
2. Perfect Management page for end users (probably easy to write HTML pages)
3 first-class URL design (nothing more than the URL can be controlled freely, although I think of it, Java EE inside the Web file does have some complex)
4. Design a friendly template language (template language, a little bit of understanding when I started to touch)
Architecture Design :
Django is a framework based on MVC constructs. But in Django, the part that the controller accepts user input is handled by the framework itself, so Django is more concerned with the model, the template, the view. Can be called MTV mode. Their respective responsibilities are as follows:
level |
Duty |
Model Layer (M), which is the data access layer |
Transactions related to data processing: Accessing data, verifying validity, what behaviors are included (increased and censored), and relationships between data |
Template (T), Business logic Layer |
Handle decisions related to performance; How to display in a page or other type of document |
View (v), presentation layer |
Access the model and adjust the appropriate logical relationship. A bridge between models and templates. |
working mechanism : (After the Django file is written, the execution mechanism of the page)
1. In cmd, Python manage.py Runserver launches the Django server and loads the settings.py in the Unified directory. The file contains configuration information in the project, such as URLs, the most important of which is root_urlconf, which tells Django which Python module should be enabled URLCONF, which is urls.py by default.
2. When accessing the URL, Django will load the urlconf based on the root_urlconf settings.
3. Then sequentially match the urlpatterns in urlconf. If found, the associated view function is called and the HttpRequest object is used as the first argument (usually request).
4. Finally the view function is responsible for returning a HttpResponse object.
Well, Django's initial appearance, we have a little bit of understanding, no matter where he is sacred, and whether he is monsters, only to start , we can really overcome it, and finally again, although it is a anti-human thing, but the function is very powerful.
001.1: Install Django
After you have installed Python and configured the environment, enter it under cmd: Pip install Django to install successfully.
001.2: New Django Project
After installing Django, on cmd, specify a folder, type: Django-admin startproject XXX (for project file name) (especially note that the crossbar between Django and admin is the middle bar in English, not the lower crossbar)
001.3: Edit Project
Simple project:
1. Under the project folder you created, there is a folder with the same name as the project, click Open, New our core part view.py
Content: from django.http import HttpResponse
def hello (Request):
Return HttpResponse ("HelloWorld")
2. Modify the contents of the urls.py, delete all the original content, add the content
From Django.conf.urls import URL
From. Import View
Urlpatterns=[url ('/', View.hello)]
001.4: Run a simple project
Open cmd into the project file in the manage.py directory, enter the Python manage.py runserver;
001.5 Viewing results
Open Browser, enter http://127.0.0.1:8000
The result is: HelloWorld.
This simple example, we look at the above working mechanism, will be enlightened. In other words, urls.py feel a bit like the main function of the program, is the entrance, and control process. Do not know the specific, continue to learn further in the back will be more understanding.
Knowledge point 1: the URL () function after urlpattern=;
URL (Regex,view[,kwargs],[name]) function: You can receive four parameters:
Regex: The URL that matches it executes the following view function
View: Execute Request
Kwargs: Using dictionary-type parameters
Name: Used to reverse get the current URL.