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 'trepeatyoursef ). 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: https://docs.djangoproject.com/en/1.4/intro/tutorial01/
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: http://httpd.apache.org/download.cgi (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
Participation:
Official Homepage: http://httpd.apache.org/
Installation documentation: http://httpd.apache.org/docs/2.2/install.html
2.2 Python installation
Download: http://www.python.org/getit/releases/2.7.3/ (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
Participation:
Official Homepage: http://www.python.org/
2.3 install the mod_wsgi module
Downlink: http://code.google.com/p/modwsgi/ (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/mod_wsgi.so... # HTTP request processing script WSGIScriptAlias/test/home/xxx/www/test. wsgi
Order allow, denyAllow from all
2.3.2 write test. wsgi (WSGI standard: 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]
2.3.3 restart apche2
Enter http://www.mysite.com/testin any web browser. See "Hello World !", Congratulations! you have successfully installed the WSGI module.
Participation:
Official Homepage: http://code.google.com/p/modwsgi/
Installation documentation: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
Profile: http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
WSGI document: http://www.python.org/dev/peps/pep-3333/
2.4 install Django
Downlink: https://www.djangoproject.com/download/ (version 1.4 selected)
Decompress: $ tar xvfz Django-1.4.tar.gz
$ Django-1.4 cd
Installation: $ python setup. py install
Test:
$python>>> import django>>> print(django.get_version())
Participation:
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 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 -*-?
HTML template file head section, add
Check whether your project's database string encoding is UTF-8, the command is 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
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 www.xxx.com/, it is transferred to django.wsgiwsgiscriptalias //.../www/my-site/apache/django. wsgi
Order deny, allowAllow from all
# Configure the access path of the static file Alias/static //.../www/my-site/static/
Order deny, allowAllow from all
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/'$./manager.py 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: https://docs.djangoproject.com/en/1.4/
Django Book (http://www.djangobook.com/en/2.0/)
Django Book Chinese: http://djangobook.py3k.cn/2.0/