Python [4] Introduction to Django's installation and basic operating environment

Source: Internet
Author: User
Tags install django

First, Django Introduction

Django is an open-source Web application framework written by Python. The MVC Software Design pattern is used, i.e. model M, view V and Controller C. It was originally developed to manage some of the news content-based websites of the Lawrence Publishing Group, namely CMS (Content management system) software. and was released in July 2005 under the BSD license. The framework is named after the Belgian gypsy jazz guitarist Django Reinhardt.


Second, the application of PIP

(1) What is PIP???

Pip is a tool for installing and managing Python packages and is a replacement for Easy_install.

Distribute is replaced by Setuptools (the Setuptools package is no longer maintained), and Pip is replaced by Easy_install.

PIP installation requires Setuptools or distribute, if you are using python3.x then you can only use distribute because python3.x does not support Setuptools.

(2) Two ways to install PIP

Mode 1: Install pip# rpm-ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm# sed-i ' by Yum Way [ Email protected]^#@@ '/etc/yum.repos.d/epel.repo# sed-i ' [email protected]@#[email protected] '/etc/yum.repos.d/ epel.repo# yum-y Install PYTHON-PIP mode 2: Compile by source pip# wget--no-check-certificate https://pypi.python.org/packages/ source/s/setuptools/setuptools-1.4.2.tar.gz# Tar XF setuptools-1.4.2.tar.gz # cd setuptools-1.4.2# python setup.py Install # Easy_install--version# CD. /# wget--no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-1.4.1.tar.gz# Tar XF pip-1.4.1.tar.gz # cd pip-1.4.1# python setup.py install

(3) Basic use of PIP

Install a specific version of the package by using = =, >=, <=, <, to specify a version number. PIP installs Django and specifies installed version # pip install ' django==1.6.5 ' pip uninstall django# pip uninstall DJANGOPIP Query package # PIP search ' Django ' pip Upgrade Pack # PIP Install-u ' Django ' lists pip installed packages # pip freeze


Third, install Django

There are two ways to install Django in a Linux environment

(1) Installing Django via PIP tool

# pip install ' django==1.6.5 '

(2) Compile and install Django via source code

# tar XF django-1.6.10.tar.gz # cd django-1.6.10# python setup.py install

(3) Import the Django module and view the version number if there is no error output indicating that the Django installation was successful

[Email protected] ~]# python-c ' Import django;print (Django.get_version ()) ' 1.6.10


Iv. Django creation projects and applications

(1) Create a project

[[email protected] ~]# django-admin.py startproject  webproject[[email protected] ~]# tree ././└── webproject# The outer directory is just a container for your project and can be renamed arbitrarily     ├── manage.py     #一个实用的命令行工具 that allows you to do it in a variety of ways with the  Django  The project Interactive     └── webproject# catalog is the actual  Python  package in your project. The directory name is the  Python  package name, through which you can import everything inside it         ├── __init__. py# an empty file, tell  Python  the directory is a  Python  pack         ├──  settings.py# the setup/configuration of the  Django  project         ├── urls.py      #该  Django  Project  URL  statement;  a  Django -driven website "directory"          └── wsgi.py# a  WSGI  compatible  Web  server portal, In order to run your project 2 directories, 5 files 

(2) Create an application

[[Email protected] ~]# CD Webproject/[[email protected] webproject]# django-admin.py startapp blog[[email protected] Webp roject]# Tree.    ├──blog│├──admin.py│├──__init__.py│├──models.py│├──tests.py│└──views.py├──manage.py└──webproject ├──__init__.py├──settings.py├──urls.py└──wsgi.py

(3) Modifying the Django configuration file

#需要修改三处位置 [[email protected] ~]# vim Webproject/settings.pyinstalled_apps = (' Django.contrib.admin ', ' Django.contrib . Auth ', ' django.contrib.contenttypes ', ' django.contrib.sessions ', ' django.contrib.messages ', ' DJANGO.CONTRIB.S Taticfiles ', ' blog ', #第一处添加blog应用) language_code = ' zh-cn ' #第二处修改为中文TIME_ZONE = ' Asia/shanghai ' #第三处修改时区

(4) Configure URL access path

[[email protected] ~]# vim webproject/urls.pyurlpatterns = Patterns (", # Examples: # URL (r ' ^$ ', ' webproject.views.h ome ', name= ' home '), # URL (r ' ^blog/', include (' Blog.urls ')), url (r ' ^admin/', include (Admin.site.urls)), url (r ' ^blo g$ ', ' Blog.views.index '), #添加一行 # Regular match, only accesses the ^blog directory to redirect to the index method in Blog.views, so the index function/method should be defined in views.py.

(5) Create a view

[Email protected] webproject]# vim blog/views.pyfrom django.shortcuts import renderfrom django.http import HttpResponse # Create your views Here.def index (req): Return HttpResponse (' 

(6) Running the Django service

[email protected] webproject]# python manage.py runserver 0.0.0.0:80validating models ... 0 Errors foundfebruary, 2015-11:58:44django version 1.6.10, using Settings ' webproject.settings ' starting development Server at Http://0.0.0.0:80/Quit the server with Control-c. [11/feb/2015 11:59:23] "get/http/1.1" 404 2003[11/feb/2015 11:59:28] "Get/blo http/1.1" 404 2012[11/feb/2015 11:59:30] "Get/blog HTTP/1.1" 20 0 23


V. Browser access

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/59/A9/wKiom1Ta17eR3THHAAD5nE1KEXk317.jpg "title=" 1.png " alt= "Wkiom1ta17er3thhaad5ne1kexk317.jpg"/>


Vi. template Layer (templates)

The above is to return a page by calling the index method in Blog.views!!!

From django.http Import httprespomsedef index (req): Return httprespose (

This method is obviously not suitable for practical applications, so let's talk about how HTML code is nested in Django in two ways:

Static page

Way One:

The template is loaded, the context object is generated, the data required for the template is stored, the data is rendered through the template object, and then the output is HttpResponse.


Template files are placed under the Application Templates directory, this directory does not exist by default need to manually create


(1) First look at the current location and the overall project directory file structure

[Email protected] webproject]# pwd/root/webproject[[email protected] webproject]# tree. ├──blog# application │?? ├──admin.py│?? ├──admin.pyc│?? ├──__init__.py│?? ├──__init__.pyc│?? ├──models.py│?? ├──models.pyc│?? ├──tests.py│?? ├──views.py│??    └──views.pyc├──manage.py└──webproject# Project ├──__init__.py├──__init__.pyc├──settings.py├──settings.pyc ├──URLS.PY├──URLS.PYC├──WSGI.PY└──WSGI.PYC2 directories, files

(2) Create our second application www.

[[email protected] webproject]# django-admin.py startapp www[[email protected] webproject]# lsblog manage.py webproject Www

(3) Update Project profile, publish new app and set URL

[[email protected] webproject]# vim webproject/settings.pyinstalled_apps =  (      ' django.contrib.admin ',     ' Django.contrib.auth ',      ' django.contrib.contenttypes ',     ' django.contrib.sessions ',      ' django.contrib.messages ',     ' django.contrib.staticfiles ',     ' Blog ',     ' www ', #添加新行) [[email protected] webproject]# vim webproject/ Urls.pyurlpatterns = patterns ("',     # examples:    #  url (R ' ^$ ',  ' webproject.views.home ',  name= ' home '),     # url (R ' ^blog/',  include (' Blog.urls ')),     url (R ' ^admin/',  include (admin.site.urls)),     url (R ' ^blog$ ', ' Blog.views.index '),     url (R ' ^www$ ', ' Www.views.index '), # Add New Line)

(4) Create a template directory and generate an HTML file

[Email protected] webproject]# mkdir www/templates[[email protected] webproject]# cat www/templates/index.html <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

(5) Modify the application view file views

[Email protected] webproject]# vim www/views.py from django.shortcuts import renderfrom django.template import Loader,co ntext# Import Django two objects loader and contextfrom django.http import httpresponse# Create your views here.def index (req): t = Loade    R.get_template (' index.html ') #导入模板文件www/templates/index.html c = Context ({}) #创建Context对象 for storing the data provided to the template (for Dynamic Web pages) Return HttpResponse (T.render (c))

(6) Browser access

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/59/AD/wKiom1TbEgSTf3FkAADYdYP4iXE356.jpg "title=" 2.png " alt= "Wkiom1tbegstf3fkaadydyp4ixe356.jpg"/>


Mode two: Modify the way one (just modify the fifth step, the other same)

[email protected] webproject]# cat www/views.pyfrom django.shortcuts import renderfrom django.shortcuts import Render_ to_response# Create Your Views here.def index (req): Return render_to_response (' index.html ', {})


This article is from the "Zheng" blog, make sure to keep this source http://467754239.blog.51cto.com/4878013/1613612

Python [4] Introduction to Django's installation and basic operating environment

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.