In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the basic knowledge and basic application introduction of Django". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the basic knowledge and basic application of Django.
This paper gives an example to describe the basic knowledge and application of Django. Share with you for your reference, the details are as follows:
MVC mode and MTV mode
MVC model view controller
MTV model templates view controller
The essence of the MTV pattern of Django is that in order to maintain loose coupling between components, the MTV of Django represents:
Model (model): objects responsible for business objects and databases (ORM)
Template (template): responsible for showing the page to the user
View (View): responsible for the business logic and invoking Model and Template when appropriate
In addition, Django has a url dispenser that distributes URL page requests to different view processes, and view calls the corresponding Model and Template.
Django basic command
Create a Django project
Django-admin startproject project_name
Create a project application
Python manage.py startapp appName
Start the Django project
Python manage.py runserver IP PORT # defaults to 8000
View django version information
Import djangoprint (django.VERSION)
Create a mysite project
Django-admin.py startproject mysite
A mysite project directory is generated under the current directory, with the following structure:
Manage.py is a tool in the Django project through which you can call django shell, databases, and so on.
Settings.py is the default settings file for the project, including database information, debug flags, and other working variables.
Urls.py is responsible for mapping url schemas to applications.
Projects and applications:
A project can have multiple applications
An application can be owned by multiple projects
Create an application in the mysite directory, such as blog
Python manage.py startapp blog
Generate the directory structure as above.
Models: files that interact with the database
Views: store the view function
Start the django project
Python manage.py runserver 8080
In this way, the project can be started and can be accessed by visiting http://127.0.0.1:8080.
Pay attention to the csrf protection mechanism
In the settings configuration file under the mysite project directory, in the middleware MIDDLEWARE settings, there is a
Django.middleware.csrf.CsrfViewMiddleware line, which can be commented out by beginners when practicing.
Next I will write a hands-on blog application in the mysite project, register and log in.
The following is the code of views.py in the blog application:
From django.shortcuts import render,HttpResponse # imports render to return the rendered web page HttpResponse can return the string import json# Create your views here.def login (request): if request.method== "POST": # specify the format as POST print (request.POST) username=request.POST.get ("user") password=request.POST.get ("pwd") f=open ("a.txt") "r") # data=f.read () dic=json.load (f) if username in dic and password==dic [username]: return HttpResponse ("login successful") # returns string content return render (request "login.html") # returns web content def auth (request): if request.method== "POST": # print (request.POST) username=request.POST.get ("user") password=request.POST.get ("pwd") info= {} info [username] = password print (info) f=open ("a.txt", "a") data=json.dump (info,f) f.close () return render (request, "auth.html")
The registration mechanism above is relatively simple, just to test the use of django.
Here is the urls.py code in the mysite directory
From django.conf.urls import urlfrom django.contrib import adminfrom blog import viewsurlpatterns = [url (r'^ admin/', admin.site.urls), url (ringing logincare journal IP:PORT/ views.login), url (ringing authentic journal journal views.auth), # login is the content after the browser url address bar IP:PORT/, corresponding to a view function]
Here are two html pages
Here's login.html.
TitleDjango login page
Name
Password
Here's auth.html.
TitleDjango Registration Page
Name
Password
The following figure shows the entire directory structure on the left and the location in the settings configuration file on the right, which identifies the template directory, otherwise it will not be found.
After the above configuration, after writing the corresponding two html pages in the templates directory, start the project through the python manage.py runserver 8900 command and access the corresponding ip port.
At this point, I believe you have a deeper understanding of the "introduction to the basic knowledge and basic application of Django". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.