Web front-End Basics-(vi) Django basics

Source: Internet
Author: User

We already know that Python's web framework has Django, Tornado, Flask and more, and Django has the advantage over other Web frameworks: Chatty, the framework itself integrates ORM, model binding, template engine, caching, session, and many more. Get up and learn about Django today.

First, the preparatory work
1) Open cmd, go to scripts;2 under Python installation path) Add environment variable using PIP Install: Pip installed Django3: Scripts under Python installation path;
Second, the basic configuration 1. Create a Django program
    • Terminal command: Django-admin startproject sitename
    • When the IDE creates a Django program, it is essentially automating the above command

The directory structure is as follows:

2. Configuration files

1) Database:

DATABASES = {'    default ': '    ENGINE ': ' Django.db.backends.mysql ',    ' NAME ': ' dbname ',    ' USER ': ' Root ',    ' PASSWORD ': ' xxx ', '    HOST ': ',    ' PORT ': ',    }}

2) Template:

Template_dirs = (        os.path.join (base_dir, ' templates '),    )

3) Static files:

Staticfiles_dirs = (        os.path.join (base_dir, ' static '),    )
third, functional classification 1. Create an app
# Switch to the Django project directory, execute the command python manage.py startapp CMDB # directory structure-CMDB    -migrations #数据库操作记录 (just modify the table structure of the record)    -init #表示 Python packet (available in Python3)    -admin #Django为我们提供的后台管理    -apps #配置当前app    -Models #创建数据库表结构, writing the specified class, via commands to To create the database structure    -tests #单元测试    -Views #写业务逻辑代码, the most important thing is this file.
2. Simple example

1) Login Instance

Generate HTML files under templates, such as login.html

<!    DOCTYPE html>

Modify a URL file to define a routing rule

From django.conf.urls import urlfrom django.contrib import adminfrom CMDB import viewsurlpatterns = [    url (r ' ^admin/'), admin.site.urls),    url (r ' ^login ', Views.login),]

Define view functions: views.py under App

From django.shortcuts import render# Create your views here.from django.shortcuts import Httpresponsefrom django.shortcut S import renderfrom django.shortcuts import redirectimport timeuser_list=[{' username ': ' Alex ', ' email ': ' [email  Protected] ', ' gender ': ' Man '}, {' username ': ' cc ', ' email ': ' [email protected] ', ' gender ': ' Male '}, {' username ': ' tt ', ' Email ': ' [email protected] ', ' gender ': ' Female '}]def Home (Request): If request.method== "POST": U = Request.        Post.get ("username"); E = Request.        Post.get ("email"); g = Request.        Post.get ("gender"); temp = {' username ': u, ' email ': E, ' Gender ': g} user_list.append (temp) return render (Request, "home.html", {"User_list" : user_list}) def login (Request): error_msg= "" If request.method== "POST": USER = Request.        Post.get (' user ', None); PWD = Request.        Post.get (' pwd ', None); If user== "root" and pwd== "111111": Return redirect ("/home/") elif user== "": error_msg= "user name cannot be Empty!     "   elif pwd = = "": error_msg = "Password cannot be empty! "else:error_msg=" username or password is wrong!    "; return render (Request, "login.html", {' error_msg ': error_msg})

Browser access http://127.0.0.1:8000/login display login.html Write login page, you can enter the login information to view the page display

2)

3) Other

Request. Get.get (", None) # Gets the data request sent from the GET requests. Post.get (", None) # Gets the data sent by the POST request return HttpResponse (" string ") return render (Request," Path to HTML template ") return redirect ('/ Can only fill in the URL ')

3. Templates

Web front-End Basics-(vi) Django basics

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.