Python Web development framework-Django (1), python-django
In the past, web. py (another lightweight web Development Framework) was used as a monitoring and management platform, so it was hard to pick up without making special records. Recently, a log aggregation system is using django. This time, we will write it down to facilitate query.
Django is an efficient web development framework that is easy to use and easy to debug and publish.
Design Mode
Its design follows the classic web design architecture-MVC (Model-view-controller). An application is divided into four parts:
- Models. py:The file mainly uses a Python class to describe the data table. It is called a model ). This article does not describe the model in detail. The database access is a single-installation mysql and self-made dao.
- Views. py:The file contains the business logic of the page. It is a bridge between templates and models. It receives front-end url requests, implements business logic, and finally returns data (results or templates ).
- Urls. py:A configuration file that maps URLs to views.
- Xxx.html:Html template, which describes the page design and supports simple template languages.
Environment preparation
Python & django, this article uses python-2.7.9, django-1.7.6
Template Language
Use the template language in html to define the page framework and content. The background is rendered into a complete html by the template engine. {%} Is used for logical control, and {} is used for variable reference.
Relationship between templates
- Include: {% include 'nav.html '%}, introducing layout units
- Template inheritance: defines a basic template (defines the page framework), which is inherited by sub-templates (fills in personalized page sub-blocks ). The basic template uses the block label-{% block %}. The content of each block may be overwritten by the quilt template. The subtemplate uses the extends tag to inherit the basic template {% extends "base.html" % }.
// Base.html <! Doctype html public "-// W3C // dtd html 4.01 // EN">
Basic control label
- {% If %} {% else %} {% endif %}: The and/or/not keyword is supported, and/or combination is not supported, comparison operations with parentheses and elif
- {% For %} {% endfor %}: the loop syntax is for X in Y.
- {% Ifequal %} {% endifequal %} Or ifnotequal: only template variables, strings, integers, and decimals can be used as parameters of the {% ifequal %} tag.
- {Var | op}: Template filter, which modifies the value of a variable before it is displayed. Op has lower/upper/length/join operations
Supplement: python empty list, empty tuples, empty dictionary, empty string, 0, None object, False object represents Boolean value False, and the rest are true boolean values.
Related Technical Points
Front-end page: HTML, JavaScript, html dom, JQuery, ajax, JSON
Backend logic: python
Page Template: We recommend that you find a bootstrap-based webpage template on the Internet.
Instance process
Project Creation
django-admin.py startproject tulip
Obtain the following directory, manage. py/_ init __. py/wsgi. py does not need to be modified. In addition, manually create static directories to save js/css, create templates to save html templates, and create lib to save dependent libraries.
tulip/ manage.py tulip/ __init__.py settings.py urls.py wsgi.py static/ templates/
Logical implementation
Write page
// Search.html <select id = "servicename" class = "form-control" onchange = "getServiceInfo () "> <option value =" "> Application service </option> {% for service in servicenames %} <option >{{ service }}</option >{% endfor %} </select>
Configure urls. py
From django. conf. urls import patterns, include, urlurlpatterns = patterns ('Tulip ', // tulip is the module name and serves as the common prefix url of the subsequent ing path (r' ^ $ ', 'searchtext. index '),)
View Logic
// SearchText. pyfrom django. shortcuts import render_to_responsefrom django. http import HttpResponsedef index (request): dao = TulipDao () servicenames = dao. getServicenames () return render_to_response('search.html ', locals () # Or return render_to_response('search.html', {'servicenames ': servicenames}) # locals () the returned dictionary contains the ing between names and values of all local variables.
Start the application
Python manage. py runserver 0.0.0.0: 8000 // 0.0.0.0 allows any non-local connection. If no ip address is specified, only the local connection is monitored.
The article is too long. Try again. The next article will record some tips and references.