Home Backend Development Python Tutorial 全面解读Python Web开发框架Django

全面解读Python Web开发框架Django

Jun 16, 2016 am 08:43 AM
django python

花了两周时间,利用工作间隙时间,开发了一个基于Django的项目任务管理Web应用。项目计划的实时动态,可以方便地被项目成员查看(^_^又重复发明轮子了)。从前台到后台,好好折腾了一把,用到:HTML、CSS、JavaScript、Apache、Python、mod_wsgi、Django。好久不用CSS和JavaScript了,感到有点生疏了,查了无数次手册。后台Django开发环境的搭建也花了不少时间和精力。记录下来,免得以后走弯路。同时给大家推荐一下Django框架,如果你想非常快速地编写自己的web应用,可以考虑使用Django,同时Django还会给你提供一个功能强大的后台管理界面。

Django是一个开源的Web应用框架,由Python写成。采用MVC的软件设计模式,主要目标是使得开发复杂的、数据库驱动的网站变得简单。Django注重组件的重用性和“可插拔性”,敏捷开发和DRY法则(Don't Repeat Yoursef)。在Django中Python被普遍使用,甚至包括配置文件和数据模型。它可以运行在启用了mod_python或mod_wsgi的Apache2,或者任何兼容WSGI(Web Server Gataway Interface)的Web服务器。

1. Django的快速开发

第一步(Model):设计自己的数据模型。
第二步(View):创建网页模板。Django自己的Html模板语言,非常容易将数据和模板结合起来,创建动态页面。
第三步(Control):定义URL,提供服务和控制。
入门教程: https://docs.djangoproject.com/en/1.4/intro/tutorial01/

2. Django开发环境的搭建

Django可以运行在任何遵守WSGI的Web服务器上。本文主要介绍Apache2+mod_wsgi+Django的环境搭建。所需要的软件如下:

Apache2:Web服务器
Python2.x:Python语言支持
mod_wsgi:Apache的WSGI模块,有了该模块的支持,就可以用Python做为CGI脚本来编写网络应用(之前还有一个mod_python,在Apache官网上发现mod_python已经过时,渐渐要被mod_wsgi替代,据说mod_wsig性能要好一些)
Django:一个强大的Python Web开发框架,本文的主角。
2.1 Apache的安装

下 载: http://httpd.apache.org/download.cgi  (选择版本2.2.22,mod_wsig暂不支持2.4.2)

解压缩 : $tar xvfz httpd-NN.tar.gz

$cd httpd-NN

编译配置: $./configure –with-included-apr –prefix=PREFIX #with-included-apr选项指定使用apache软件包里面的apr库

编 译: $make

安 装: $make install

配 置: $vim PREFIX/conf/httpd.conf

测 试: $PREFIX/bin/apachectl -k start

参 考:

官方主页: http://httpd.apache.org/
安装文档: http://httpd.apache.org/docs/2.2/install.html
2.2 Python的安装

下 载: http://www.python.org/getit/releases/2.7.3/ (选择2.X版都可以,3.0暂不支持)

解压缩 : $tar xvf python-X.tar

$cd python-Y

编译配置: $./configure –enable-shared –prefix=PREFIX #–enable-shared选项指定生成python的动态库

编 译: $make

安 装: $make install

测 试: $python

参 考:

官方主页: http://www.python.org/
2.3 mod_wsgi模块的安装

下 载: http://code.google.com/p/modwsgi/  (选择3.3版本)

解压缩 : $tar xvfz mod_wsgi.X.Y.tar.gz

$cd mod_wsgi.X.Y

编译配置: $././configure –with-apxs=/usr/local/apache2/bin/apxs –with-python=/usr/local/bin/python # 指定Apache2的模块编译程序和Python解析器

编 译: $make

安 装: $make install

测 试: $python

2.3.1  配置Apache(修改/usr/local/apche2/confi/httpd.conf)

# 加载wsgi模块
LoadModule wsgi_module modules/mod_wsgi.so
....
# HTTP请求处理脚本
WSGIScriptAlias /test /home/xxx/www/test.wsgi
<Directory "/home/xxx/www">
Order allow, deny
Allow from all
</Directory>
Copy after login

2.3.2 编写test.wsgi(WSGI标准: http://www.python.org/dev/peps/pep-3333/ )

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

 response_headers = [('Content-type', 'text/plain'),
   ('Content-Length', str(len(output)))]
 start_response(status, response_headers)

 return [output]
Copy after login

2.3.3 重启apche2

在任意网络浏览器中输入:http://www.mysite.com/test。看到“Hello World!”,恭喜你成功安装了WSGI模块。

参 考:

官方主页: http://code.google.com/p/modwsgi/
安装文档: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
配置文档: http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
WSGI文档: http://www.python.org/dev/peps/pep-3333/

2.4 Django的安装

下 载: https://www.djangoproject.com/download/ (选择1.4版本)

解压缩 : $tar xvfz Django-1.4.tar.gz

$cd Django-1.4

安 装: $python setup.py install

测 试:

$python
>>> import django
>>> print(django.get_version())
Copy after login

参 考:

官方主页: https://www.djangoproject.com/
安装文档: https://docs.djangoproject.com/en/1.4/intro/install/
快速入门: https://docs.djangoproject.com/en/1.4/intro/tutorial01/

3. Django中文支持

Django使用的是UTF-8编码,所以对于国际化支持不成问题。因为初次玩Django,中文显示乱,折腾死人了(一直在用的的mysql默认字符串是latin1编码,vim默认保存的文件编码为ascii)。最终得出结论,如果中文显示乱码,或者Django报错(… unicode …blabla…),请检查:

Django的设置。打开自己项目的settings.py,LANGUAGE_CODE=”zh_CN” ?FILE_CHARSET='UTF-8′ ?DEFAULT_CHARSET='utf-8′?
查看自己项目所有的文件编码是否以UTF-8编码保存的?确保.py文件第一行要加上:#-*- coding:utf-8 -*- ?
HTML模板文件head部分,添加
检查自己项目的数据库字符串编码是否为UTF-8,命令如下:
查看:

show create database dbname; 
show create table tablename; 
show full columns from tablename; 
Copy after login

创建:

create database dbname CHARACTER SET utf8; 
create table tblname CHARACTER SET utf8; 
Copy after login

修改:

alter database dbname CHARACTER SET = utf8; 
alter table tablename CONVERT TO CHARACTER SET utf8;
Copy after login

4. Django应用的部署

Django应用的运行有两个方式,一种是在开发阶段,使用创建项目下面的manager.py runserver ip:port来启动一个用Python实现的轻型web服务器;另外一种就是通过mod_wsgi将你自己的应用部署到生产环境,对外提供服务。下面简单介绍一下Django的部署(虚拟主机上的配置,自行参考文档)。

假设你创建的Django项目文件列表如下:

my-site
|- my-site
|- myapp
 |-static
 |- ...
|- static
 |- css
 |- js
 | ...
|- apache
|- ...
Copy after login

4. 1. 创建Django项目的wsgi脚本(my-site/apache/django.wsgi),内容如下:

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)
Copy after login

4.2. 配置Apache(httpd.conf),内容如下:

# 请求访问www.xxx.com/的时候,转到django.wsgi
WSGIScriptAlias / /.../www/my-site/apache/django.wsgi

<Directory /.../www/my-site/apache>
Order deny,allow
Allow from all
</Directory>

# 静态文件的访问路径配置
Alias /static/ /.../www/my-site/static/

<Directory /.../www/my-site/static>
Order deny,allow
Allow from all
</Directory>
Copy after login

4.3. 配置setting.py

EBUG=False
自定义404.html,500.html模板(网页未找到、服务器内部错误)

4.4. 静态文件

STATIC_ROOT = ‘/…/www/my-site/static/'
STATIC_URL = ‘/static/'
$./manager.py collectstatic
Copy after login

注意:开发阶段,一般都会把相应app的静态文件,放在app目录下的static目录下。在正式生产环境部署的时候,使用./manager.py collectstatic来把所有静态文件收集到STATIC_ROOT指定的位置,包括管理后台的。

4.5. 重启apahce

浏览器输入相应的URL地址,看到你自己的web应用界面的话,恭喜大功告成!

5. 总结

本文主要介绍了一下Django开发环境的搭建、Django应用的部署和中文乱码的解决方法。具体如何使用Django快速地创建自己的web应用,并没有提及。Django相对来说,文档比较齐全,加上官方推出的一本书:《The Django Book》,相信只要开发环境搭建好,创建自己的Web应用也会非常容易。

进一步学习Django,请看:

Django1.4文档: https://docs.djangoproject.com/en/1.4/
Django Book 英文版: http://www.djangobook.com/en/2.0/
Django Book 中文版: http://djangobook.py3k.cn/2.0/

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

MySQL can't be installed after downloading MySQL can't be installed after downloading Apr 08, 2025 am 11:24 AM

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

MySQL download file is damaged and cannot be installed. Repair solution MySQL download file is damaged and cannot be installed. Repair solution Apr 08, 2025 am 11:21 AM

MySQL download file is corrupt, what should I do? Alas, if you download MySQL, you can encounter file corruption. It’s really not easy these days! This article will talk about how to solve this problem so that everyone can avoid detours. After reading it, you can not only repair the damaged MySQL installation package, but also have a deeper understanding of the download and installation process to avoid getting stuck in the future. Let’s first talk about why downloading files is damaged. There are many reasons for this. Network problems are the culprit. Interruption in the download process and instability in the network may lead to file corruption. There is also the problem with the download source itself. The server file itself is broken, and of course it is also broken when you download it. In addition, excessive "passionate" scanning of some antivirus software may also cause file corruption. Diagnostic problem: Determine if the file is really corrupt

Solutions to the service that cannot be started after MySQL installation Solutions to the service that cannot be started after MySQL installation Apr 08, 2025 am 11:18 AM

MySQL refused to start? Don’t panic, let’s check it out! Many friends found that the service could not be started after installing MySQL, and they were so anxious! Don’t worry, this article will take you to deal with it calmly and find out the mastermind behind it! After reading it, you can not only solve this problem, but also improve your understanding of MySQL services and your ideas for troubleshooting problems, and become a more powerful database administrator! The MySQL service failed to start, and there are many reasons, ranging from simple configuration errors to complex system problems. Let’s start with the most common aspects. Basic knowledge: A brief description of the service startup process MySQL service startup. Simply put, the operating system loads MySQL-related files and then starts the MySQL daemon. This involves configuration

Does mysql need the internet Does mysql need the internet Apr 08, 2025 pm 02:18 PM

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

How to optimize database performance after mysql installation How to optimize database performance after mysql installation Apr 08, 2025 am 11:36 AM

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

See all articles