---
title: "install django"
author: "pqcc"
date: "2010-05-08T15:35:34Z"
tags: []
categories: ["编程语言"]
slug: "install-django"
draft: false
lastmod: "2010-05-08T15:35:34Z"
id: 661958
url: "https://pqcc.iteye.com/blog/661958"
---

Refrence: [http://fullraith.com/content/788.html](http://fullraith.com/content/788.html)

1. Download  tar file from : [http://www.djangoproject.com/download/](http://www.djangoproject.com/download/)  
2. unzip the file and install:  
 tar zxvf Django-0.96.tar.gz  
 cd Django-0.96  
 sudo python setup.py install   
3. Test the django is ok ?  
 $yanqing&gt;python  
 &gt;&gt;&gt;import django  
 // If not throw exception,it ok. In fact, the django is a package of django.

4. Create project: \[暂时取名: djangotest,缺省 apache2 的文档目录是/var/www.]  
 (1). If the django installed, it'll touch file /usr/bin/django-admin.py  
 (2). python /usr/bin/django-admin.py startproject gtdcast  
 (3). cd gtdcast  
   ls  
   // it will display the file  
   \_inti\_.py: 表示这是一个 Python 的包  
   manage.py: 供简单化的 django-admin.py 命令，特别是可以自动进行 DJANGO\_SETTINGS\_MODULES 和 PYTHONPATH 的处理，而没有这个命令，处理上面环境变量是件麻烦的事情.  
   settings.py  
   uls.py:  url映射处理文件，而 Django 的url映射是url对于某个模块方法的映射，目前不能自动完成  
 (4). sudo python manage.py runserver:  
  /\** Validating models...  
   0 errors found.

   Django version 0.96.3, using settings 'djangotest.settings'  
   Development server is running at [http://127.0.0.1:8000/](http://127.0.0.1:8000/)  
   Quit the server with CONTROL-C. \*\*/  
     
 (5). Visit the [http://127.0.0.1:8080/](http://127.0.0.1:8080/).  
   
\---------------------------  more ---------------------------------------  
5. apached mod\_python install:  
 (1). sudo apt-get install apache2  
 (2). sudo apt-get install libapache2-mod-python2.5  
 (3). sudo vi /etc/apache2/site-avaiable/default 添加:  
   &lt;Directory /var/www&gt;  
    SetHandler python-program  
    PythonPath "\['/var/www'] + sys.path"  
    PythonHandler django.core.handlers.modpython  
    SetEnv DJANGO\_SETTINGS\_MODULE djangotest.settings  
    PythonDebug On  
   &lt;/Directory&gt;  
 (4). 重启 apache /etc/init.d/apache2 restart  
    
6. 进入 gtdcast 目录:  
 \_init\_.py: 表示这是一个 Python 的包。  
 manage.py:   
 settings.py:  
 urls.py:   
   
7. Start server:  
  manage.py runserver   
8. 请注意 action 为 /add/ ，在 Django 中链接后面一般都要有 '/' ，不然有可能得不到 POST 数据。有关更详细的关于常见问题可以参阅 NewbieMistakes 文档  
 [http://code.djangoproject.com/wiki/NewbieMistakes](http://code.djangoproject.com/wiki/NewbieMistakes)  
 Django 的设计风格中认为: 使用 POST ----&gt;,GET-----&gt;获取。  
 (URL Dispatcher:http://docs.djangoproject.com/en/dev/topics/http/urls/?from=olddocs)  
   
9. 模板系统:   
 (1). def index(request):  
      return render\_to\_response('list.html', {'address': address})  
     // Django 0.91 中，模板文件都是以.html结尾的，并且使用时不带后缀的。但0.95版本取消了模板后缀的设置,因此模板文件名必须是完整的，不再有默认后缀名了。  
    (2). Django 还支持在App中定义一个 templates目录。这样Django在启动时会检查所有安装的App的templates，如果存在，则将路径的搜索放在 TEMPLATE\_DIRS之后.  
    (3). Django模板中{ {} }表示引用一个变量。  
        { %% } 表示代码调用。  
     (Built-in template tags and filters: [http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs](http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs))  
    (4). for in  
      {% for user in address %}  
       {{ user.name }}  
       {{ user.address }}  
      &lt;% endfor%&gt;