A comprehensive interpretation of the Python Web development Framework Django_python

Source: Internet
Author: User
Tags documentation http request create database in python

It took two weeks to develop a Django based project task Management Web application using the working gap time. The real-time dynamics of the project plan can be easily viewed by Project members (^_^ again invent the wheel). From the front to the backstage, a good toss of the use of: HTML, CSS, JavaScript, Apache, Python, Mod_wsgi, Django. For a long time without CSS and JavaScript, feel a little rusty, checked countless manuals. The background Django development environment construction also spent a lot of time and effort. Record them, lest they should be detours later. Also recommend the Django framework, if you want to write your own Web application very quickly, consider using Django, and Django will also provide you with a powerful background management interface.

Django is an open source Web application framework, written by Python. Using MVC's software design pattern, the main goal is to make it easy to develop complex, database-driven Web sites. Django focuses on component reusability and "pluggable", agile development and dry rules (Don T Repeat Yoursef). Python is commonly used in Django, even including configuration files and data models. It can run on Mod_python or MOD_WSGI-enabled Apache2, or any Web server that is compatible with WSGI (Web server Gataway Interface).

1. Django's rapid development

The first step (model): Design your own data model.
Second Step (View): Create a Web page template. Django's own HTML template language, it's easy to combine data and templates to create dynamic pages.
Step three: Define URLs, provide service and control.
Getting Started Tutorial: Http://wiht.link/django_primer

2. Django Development Environment Setup

Django can run on any Web server that adheres to WSGI. This paper mainly introduces the environment of Apache2+mod_wsgi+django. The required software is as follows:

Apache2:web Server
Python2.x:python Language Support
Mod_wsgi:apache Wsgi module, with the support of the module, you can use Python as a CGI script to write network applications (there was a mod_python, found in the Apache online Mod_python has become obsolete, gradually to be mod_ Wsgi substitution, it is said that Mod_wsig performance is better)
Django: A powerful Python web development framework, the protagonist of this article.
Installation of 2.1 Apache

Download: http://httpd.apache.org/download.cgi (select version 2.2.22,mod_wsig temporarily do not support 2.4.2)

Decompression: $tar XVFZ httpd-nn.tar.gz

$CD Httpd-nn

Compilation configuration: $./configure–with-included-apr–prefix=prefix #with-included-apr option specifies the APR library inside the Apache package

Compiling: $make

Installation: $make Install

Configuration: $vim prefix/conf/httpd.conf

Test: $PREFIX/bin/apachectl-k start

Reference

Official homepage: http://httpd.apache.org/
Installation Documentation: http://httpd.apache.org/docs/2.2/install.html
Installation of 2.2 Python

Download: http://www.python.org/getit/releases/2.7.3/(option 2. X version can be, 3.0 temporarily not supported)

Decompression: $tar xvf Python-x.tar

$CD python-y

Compilation configuration: $./configure–enable-shared–prefix=prefix #–enable-shared option specifies the dynamic library that generates Python

Compiling: $make

Installation: $make Install

Test: $python

Reference

Official homepage: http://www.python.org/
Installation of 2.3 Mod_wsgi module

Download: http://code.google.com/p/modwsgi/(choose 3.3 version)

Decompression: $tar XVFZ Mod_wsgi. X.y.tar.gz

$CD Mod_wsgi. x.y

Compile configuration: $././configure–with-apxs=/usr/local/apache2/bin/apxs–with-python=/usr/local/bin/python # Specifies the Apache2 module compiler and the Python parser

Compiling: $make

Installation: $make Install

Test: $python

2.3.1 Configure Apache (Modify/usr/local/apche2/confi/httpd.conf)

# load WSGI module
LoadModule wsgi_module modules/mod_wsgi.so
....
# HTTP request processing Script
Wsgiscriptalias/test/home/xxx/www/test.wsgi
<directory "/home/xxx/www" >
Order Allow, deny
allow from all
</Directory>

2.3.2 Writing Test.wsgi (WSGI standard: http://www.python.org/dev/peps/pep-3333/)

def application (environ, start_response):
 status = ' OK '
 output = ' Hello world!

 ' Response_headers = [(' Content-type ', ' Text/plain '),
  (' Content-length ', str (output))]
 Start_response (Status, Response_headers)

 return [Output]

2.3.3 Reboot Apche2

Enter in any Web browser: http://www.mysite.com/test. See "Hello world!", congratulations on your successful installation of the Wsgi module.

Reference

Official homepage: http://code.google.com/p/modwsgi/
Installation Documentation: Http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
Configuration document: Http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
WSGI Documents: http://www.python.org/dev/peps/pep-3333/

2.4 Django Installation

Download: https://www.djangoproject.com/download/(choose 1.4 version)

Decompression: $tar XVFZ django-1.4.tar.gz

$CD Django-1.4

Installation: $python setup.py Install

Test

$python
>>> import Django
>>> print (django.get_version ())

Reference

Official homepage: https://www.djangoproject.com/
Installation Documentation: https://docs.djangoproject.com/en/1.4/intro/install/
Quick Start: https://docs.djangoproject.com/en/1.4/intro/tutorial01/

3. Django Chinese support

Django uses UTF-8 encoding, so it's not a problem for internationalization support. Because the first time to play Django, Chinese display chaos, toss the dead (always in the MySQL default string is latin1 encoding, vim default saved file encoding for ASCII). Finally come to the conclusion, if the Chinese display garbled, or Django error (... Unicode ... blabla ...), please check:

The Django settings. Open your own project's settings.py,language_code= "ZH_CN"? File_charset= ' utf-8′? Default_charset= ' utf-8′?
View all file encodings of your own project saved with UTF-8 encoding? Ensure that the. py file is added to the first line: #-*-coding:utf-8-*-?
HTML template file Head section, add <meta http-equiv= "Content-type" content= text/html;charset=utf-8″/>
Check that the database string encoding for your project is UTF-8, as follows:
View:

Show create database dbname; 
Show CREATE TABLE tablename; 
Show full columns from TableName; 

Create:

Create DATABASE dbname CHARACTER SET UTF8; 
CREATE TABLE tblname CHARACTER SET UTF8; 

Modify:

ALTER DATABASE dbname CHARACTER SET = UTF8; 
ALTER TABLE TableName CONVERT to CHARACTER SET UTF8;

4. Deployment of Django Applications

Django applications run in two ways, one in the development phase, using the manager.py runserver Ip:port under the Create project to start a lightweight Web server implemented in Python; the other is through Mod_ WSGI will deploy your own applications to the production environment and provide services externally. The following is a brief introduction to the Django Deployment (configuration on the virtual host, self reference documentation).

Suppose you create a list of Django project files as follows:

My-site | | my-site |-
myapp
 |-static
| -static | |-
 CSS
 |
-Apache
|

4.1. Create the Wsgi script (MY-SITE/APACHE/DJANGO.WSGI) for the Django Project, which reads as follows:

Import OS, sys

sys.path.append ('/.../www/')
sys.path.append ('/.../www/my-site ')
os.environ[' Django_ Settings_module '] = ' my-site.settings '
os.environ[' python_egg_cache '] = '/.../www/.python-eggs '

import Django.core.handlers.wsgi

_application = Django.core.handlers.wsgi.WSGIHandler ()

def application (environ, Start_response):
 if environ[' wsgi.url_scheme '] = = ' https ':
  environ[' https '] = ' on ' Return
 _application ( Environ, Start_response)

4.2. Configure Apache (httpd.conf), which reads as follows:

# when requesting access to www.xxx.com/, go to Django.wsgi
wsgiscriptalias//.../www/my-site/apache/django.wsgi

<directory /.../www/my-site/apache> order
deny,allow
allow

access path configuration for static files in all </Directory> #
alias/static//.../www/my-site/static/

<directory/.../www/my-site/static> order
Deny,allow
Allow from all
</Directory>

4.3. Configure setting.py

Ebug=false
Custom 404.html,500.html Template (Web page not found, server internal error)

4.4. Static file

Static_root = '/.../www/my-site/static/'
static_url = '/static/'
$./manager.py collectstatic

Note: In the development phase, the corresponding app's static files are usually placed under the static directory under the app directory. When the formal production environment is deployed, use the./manager.py collectstatic to collect all the static files to the Static_root specified location, including the admin backend.

4.5. Restart APAHCE

Browser to enter the appropriate URL address, see your own Web application interface, congratulations!

5. Summary

This article mainly introduces the design of the Django development environment, the deployment of Django application and the solution of Chinese garbled method. Specifically how to use Django to quickly create your own Web application is not mentioned. Django relatively complete documentation, plus an official book: The Django Books, I believe that as long as the development environment is built, it will be easy to create their own web applications.

To learn more about Django, see:

Django1.4 Documents: https://docs.djangoproject.com/en/1.4/
Django Book English: http://www.djangobook.com/en/2.0/
Django book Chinese version: http://djangobook.py3k.cn/2.0/

Related Article

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.