Django in detail
Django is an open source web application framework written by PYTHON. Using the software design pattern of MVC, the main goal is to make it easy to develop complex, database-driven websites. Django focuses on the reusability and "pluggability" of components, agile development and the DRY principle (Don't Repeat Yoursef).
It took two weeks to develop a Django-based project task management web application using the spare time from work. The real-time dynamics of the project plan can be easily viewed by project members (^_^ reinvented the wheel again). From the foreground to the background, I have tossed a lot, using: HTML, CSS, JAVAScript, Apache, PYTHON, mod_wsgi, Django. I haven't used CSS and JAVAScript for a long time, I feel a little rusty, and I checked the manual countless times. The construction of the background Django development environment also took a lot of time and effort. Record it to avoid detours later. At the same time, I recommend the Django framework to everyone. If you want to write your own web application very quickly, you can consider using Django. At the same time, Django will also provide you with a powerful background management interface.
Django is an open source web application framework written by PYTHON. Using the software design pattern of MVC, the main goal is to make it easy to develop complex, database-driven websites. Django focuses on the reusability and "pluggability" of components, agile development and the DRY principle (Don't Repeat Yoursef). PYTHON is widely used in Django, even including configuration files and data models. It can run on Apache2 with mod_PYTHON or mod_wsgi enabled, or any WSGI (Web Server Gataway Interface) compatible web server.
1. Rapid development of Django
The first step (Model): Design your own data model. Step 2 (View): Create a web page template. Django's own Html template language makes it easy to combine data and templates to create dynamic pages. The third step (Control): define the URL, provide services and control. Getting Started Tutorial: GO_primer" target="_blank"> http://wiht.link/djanGO_primer
2. Setting up the Django development environment
Django can run on any WSGI-compliant web server. This article mainly introduces the environment construction of Apache2+mod_wsgi+Django. The required software is as follows:
Apache2: Web server PYTHON2.x: PYTHON language support mod_wsgi: Apache's WSGI module, with the support of this module, you can use PYTHON as a CGI script to write network applications (there was also a mod_PYTHON before, On the Apache official website, I found that mod_PYTHON is outdated and will gradually be replaced by mod_wsgi. It is said that mod_wsig has better performance) DjanGO: A powerful PYTHON Web development framework, the protagonist of this article. 2.1 Apache installation and
download: http://httpd.apache.org/download.cgi (select version 2.2.22, mod_wsig does not support 2.4.2 temporarily)
Unzip: $tar xvfz httpd-NN.tar.gz
$cd httpd -NN
compilation configuration: $./configure –with-included-apr –prefix=PREFIX #with-included-apr option specifies to use the apr library in the apache package to
compile: $make
Installation: $make installConfiguration
: $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.html2.2 PYTHON installation
download: PYTHON.org/getit/releases/2.7.3/">http://www.PYTHON.org/getit/releases/2.7 .3/ (You can choose version 2.X, but 3.0 is not currently supported)
Unzip: $tar xvf PYTHON-X.tar
$cd PYTHON-Y
Compile and configure: $./configure –enable-shared –prefix=PREFIX #– The enable-shared option specifies to generate PYTHON's dynamic library
compilation: $make
install: $make install
test: $PYTHON
Reference:
Official homepage: PYTHON.org/">http://www.PYTHON.org/2.3 mod_wsgi module Install
and download: GOogle.com/p/modwsgi/">http://code.GOogle.com/p/modwsgi/ (select version 3.3)
Unzip: $tar xvfz mod_wsgi.XYtar.gz
$cd mod_wsgi.XY
Build configuration: $././configure --with-apxs=/usr/local/apache2/bin/apxs --with-PYTHON=/usr/local/ bin/PYTHON # Specify Apache2's module compiler and PYTHON parser to
compile: $make
install: $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 Write test.wsgi (WSGI standard: PYTHON.org/dev/peps/pep-3333/"> 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
in any web browser: http://www.mysite.com/test. When you see "Hello World!", congratulations that you successfully installed the WSGI module.
refer to:
Official homepage: GOogle.com/p/modwsgi/">http://code.GOogle.com/p/modwsgi/Installation documentation: GOogle.com/p/modwsgi/wiki/QuickInstallationGuide">http:// code.GOogle.com/p/modwsgi/wiki/QuickInstallationGuide Configuration Documentation: GOogle.com/p/modwsgi/wiki/QuickConfigurationGuide">http://code.GOogle.com/p/modwsgi/wiki/QuickConfigurationGuideWSGI Documentation: PYTHON .org/dev/peps/pep-3333/">http://www.PYTHON.org/dev/peps/pep-3333/
2.4 DjanGO installation
download: GOproject.com/download/">https ://www.djanGOproject.com/download/ (select version 1.4)
unzip: $tar xvfz DjanGO-1.4.tar.gz
$cd DjanGO-1.4
install: $PYTHON setup.py install
test:
$PYTHON
>> > import djanGO
>>> print(djanGO. get_version())
reference:
Official Homepage: GOproject.com/">https://www.djanGOproject.com/ Installation Documentation: GOproject.com/en/1.4/intro/install/">https://docs.djanGOproject.com/ en/1.4/intro/install/ Quick Start: GOproject.com/en/1.4/intro/tutorial01/">https://docs.djanGOproject.com/en/1.4/intro/tutorial01/
3. Django Chinese support
Django uses UTF-8 encoding, so internationalization support is not a problem. Because I played Django for the first time, the Chinese display was messy, and the tossing was dead (the default string of mysql that I have been using is latin1 encoding, and the default file encoding of vim is ascii). In the end, it is concluded that if Chinese display garbled characters, or Django reports errors (…unicode…blabla…), please check:
Django settings. Open settings.py of your own project, LANGUAGE_CODE=”zh_CN”? FILE_CHARSET='UTF-8' ? DEFAULT_CHARSET='utf-8'? Check if all files in your project are encoded in UTF-8? Make sure to add: #-*- coding:utf-8 -*- on the first line of the .py file? In the head section of the HTML template file, add <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> to check whether the database string encoding of your project is UTF-8, command 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. Django application There are two ways to deploy
Django applications. One is in the development phase, using manager.py runserver ip:port under the created project to start a lightweight web server implemented with PYTHON; the other is to use mod_wsgi to convert your own The application is deployed to the production environment to provide external services. The following is a brief introduction to the deployment of Django (configuration on the virtual host, refer to the documentation yourself).
Suppose you create a list of Django project files as follows:
my-site
|- my-site
|- myapp
|-static
|- ...
|- static
|- css
|- js
| ...
|- apache
|- ...
4. 1. Create the wsgi script of the Django project (my-site/apache/djanGO.wsgi), the content is 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) 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 from all
</Directory>
# for static files Access path configuration
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 to customize 404.html, 500.html templates (page not found, internal server error)
4.4. Static file
STATIC_ROOT = '/…/www/my-site/static/'
STATIC_URL = '/static /'
$./manager.py collectstatic
Note: During the development phase, the static files of the corresponding app are generally placed in the static directory under the app directory. When deploying in the official production environment, use ./manager.py collectstatic to collect all static files to the location specified by STATIC_ROOT, including the management background.
4.5. Restart the apahce
browser, enter the corresponding URL address, and if you see your own web application interface, congratulations!
5. Summary
This article mainly introduces the construction of the Django development environment, the deployment of Django applications and the solution to Chinese garbled characters. How to use Django to quickly create your own web application is not mentioned. Relatively speaking, Django has relatively complete documents, plus 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.
To learn more about Django, see:
Django1.4 Documentation: GOproject.com/en/1.4/">https://docs.djanGOproject.com/en/1.4/DjanGO Book English: GObook.com/en/2.0 /">http://www.djanGObook.com/en/2.0/DjanGO Book Chinese version: GObook.py3k.cn/2.0/">http://djanGObook.py3k.cn/2.0/
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326365805&siteId=291194637
Recommended
Ranking