Django details, django details

Source: Internet
Author: User
Tags install django

Django details, django details
Django is an open-source Web application framework written in Python. Using the MVC software design model, the main goal is to make the development of complex, database-driven websites easy. Django focuses on Component reusability and "pluggable", agile development and DRY rules (Don't Repeat Yoursef ).

It took two weeks to develop a Django-based project task management Web application using the work gap time. The real-time dynamics of the Project Plan can be easily viewed by project members (repeated invention wheel ). From the front-end to the back-end, I had a hard time using HTML, CSS, JavaScript, Apache, Python, mod_wsgi, and Django. I haven't used CSS and JavaScript For a long time, so I feel a little unfamiliar. I have checked the manuals countless times. It also took a lot of time and effort to build the background Django development environment. Record it to avoid detours in the future. At the same time, we recommend the Django framework. If you want to write your own web applications very quickly, you can consider using Django. At the same time, Django will provide you with a powerful background management interface.

Django is an open-source Web application framework written in Python. Using the MVC software design model, the main goal is to make the development of complex, database-driven websites easy. Django focuses on Component reusability and "pluggable", agile development and DRY rules (Don't Repeat Yoursef ). Python is widely used in Django, including configuration files and data models. It can run on Apache2 with mod_python or mod_wsgi enabled, or any Web Server compatible with WSGI (Web Server Gataway Interface.

1. Quick Django Development

Step 1 (Model): design your own data Model.
Step 2 (View): create a webpage template. Django's own Html template language can easily combine data and templates to create dynamic pages.
Step 3 (Control): define the URL to provide services and Control.
Getting Started:

2. Build a Django Development Environment

Django can run on any Web server that complies with WSGI. This article describes how to build an Apache2 + mod_wsgi + Django environment. The required software is as follows:

Apache2: Web Server
Python2.x: Python supported
Mod_wsgi: The WSGI module of Apache. With the support of this module, you can use Python as the CGI script to compile network applications (there was also a mod_python which was found outdated on the Apache official website, it is gradually replaced by mod_wsgi. It is said that mod_wsig has better performance)
Django: A Powerful Python Web development framework, the main character of this article.
2.1 install Apache

Downlink: (select version 2.2.22, mod_wsig does not currently support 2.4.2)

Decompress: $ tar xvfz httpd-NN.tar.gz

$ Cd httpd-NN

Compile the configuration: $./configure-with-defined ded-apr-prefix = PREFIX # with-defined ded-apr option specifies to use the apr library in the apache Software Package

Compilation: $ make

Installation: $ make install

Configuration: $ vim PREFIX/conf/httpd. conf

Test: $ PREFIX/bin/apachectl-k start


Official homepage:
Installation documentation:
2.2 Python Installation

Download: (available in version 2. X, not currently supported in version 3.0)

Decompress: $ tar xvf python-X.tar

$ Cd python-Y

Compile the configuration: $./configure-enable-shared-prefix = PREFIX #-enable-shared option to specify the dynamic library for generating python

Compilation: $ make

Installation: $ make install

Test: $ python


Official homepage:
2.3 install the mod_wsgi Module

Downlink: (version 3.3 selected)

Decompress: $ tar xvfz mod_wsgi.X.Y.tar.gz

$ Cd mod_wsgi.X.Y

Compilation configuration: $. /. /configure-with-apxs =/usr/local/apache2/bin/apxs-with-python =/usr/local/bin/python # specify the module Compilation Program of Apache2 and Python Parsing tool

Compilation: $ 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/ .... # HTTP request processing script WSGIScriptAlias/test/home/xxx/www/test. wsgi <Directory "/home/xxx/www"> Order allow, denyAllow from all </Directory>

2.3.2 write test. wsgi (WSGI standard:

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]

2.3.3 restart apche2

Enter any web browser. See "Hello World !", Congratulations! You have successfully installed the WSGI module.


Official homepage:
Installation documentation:
WSGI document:

2.4 install Django

Downlink: (version 1.4 selected)

Decompress: $ tar xvfz Django-1.4.tar.gz

$ Django-1.4 cd

Installation: $ python setup. py install


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


Official homepage:
Installation documentation:
Quick Start:

3. Django Chinese support

Django uses UTF-8 encoding, so there is no problem with internationalization support. Due to the first time I played Django, Chinese characters were messy. (The mysql Default string that I 've been using is latin1 encoding, and vim saves files as ascii by default ). The final conclusion is that if Chinese characters are garbled or Django reports an error (... Unicode... Blabla ...), Check:

Django settings. Open settings. py of your project, export age_code = "zh_CN "? FILE_CHARSET = 'utf-8 ′? DEFAULT_CHARSET = 'utf-8 ′?
Check if all file codes of your project are saved in UTF-8 encoding? Make sure that: #-*-coding: UTF-8-*-? is added to the first line of the. py file -*-?
Add <meta http-equiv = "Content-Type" content = "text/HTML; charset = UTF-8"/>
Check whether your project's database string encoding is UTF-8, the command is as follows:

show create database dbname; show create table tablename; show full columns from tablename; 


create database dbname CHARACTER SET utf8; create table tblname CHARACTER SET utf8; 


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

4. Deployment of Django applications

There are two ways to run a Django application: one is to use the manager under the project to create a project in the development stage. py runserver ip: port is used to start a lightweight web server implemented in Python. The other is to deploy your own applications to the production environment through mod_wsgi to provide external services. The following describes how to deploy Django (for configuration on the virtual host, refer to this document ).

Assume that the list of Django project files you created is as follows:

my-site|- my-site|- myapp |-static |- ...|- static |- css |- js | ...|- apache|- ...

. Create the wsgi script (my-site/apache/Django. wsgi) of the django project. The content is as follows:

import os, syssys.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) with the following content:

# When the request accesses, it is transferred to django.wsgiwsgiscriptalias //... /www/my-site/apache/django. wsgi <Directory /... /www/my-site/apache> Order deny, allowAllow from all </Directory> # configure the access path of static files Alias/static //... /www/my-site/static/<Directory /... /www/my-site/static> Order deny, allowAllow from all </Directory>

4.3. Configure setting. py

EBUG = False
Custom 404.html,500.html templates (webpage not found, internal server error)

4.4. static files

STATIC_ROOT = ‘/…/www/my-site/static/’STATIC_URL = ‘/static/’$./ collectstatic

Note: In the development phase, the static files of the corresponding app are usually stored in the static directory under the app directory. When deploying the production environment, use./manager. py collectstatic to collect all static files to the location specified by STATIC_ROOT, including the Management backend.

4.5. Restart apahce

Enter the corresponding URL address in the browser to view your web application interface. Congratulations!

5. Summary

This article describes how to build a Django development environment, deploy Django applications, and solve Chinese garbled characters. I didn't mention how to use Django to quickly create your own web application. Django is relatively complete in terms of documentation, coupled with an official Book "The Django Book", I believe that as long as The development environment is set up, it will be very easy to create your own Web applications.

For more information about Django, see:

Django1.4 documents:
Django Book (
Django Book Chinese:



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: 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.