Django

来自开放百科 - 灰狐
(版本间的差异)
跳转到: 导航, 搜索
(项目)
 
(未显示1个用户的87个中间版本)
第3行: 第3行:
 
Django: The Web framework for perfectionists with deadlines
 
Django: The Web framework for perfectionists with deadlines
  
 +
==简介==
 
Django 是一个高级 Python web framework,它鼓励快速开发和干净的、MVC设计。它包括一个模板系统,对象相关的映射和用于动态创建管理界面的框架。Django遵守BSD版权。
 
Django 是一个高级 Python web framework,它鼓励快速开发和干净的、MVC设计。它包括一个模板系统,对象相关的映射和用于动态创建管理界面的框架。Django遵守BSD版权。
  
Django 很象 [[Ruby on Rails]].  
+
Django 很像 [[Rails|Ruby on Rails]].  
  
==Django 2.0==
+
[[Image:Leaves-680x100.jpg|没有真理,只有解释。—— 尼采|right]]
[[Django 2.0]] is far more friendly for Web 2.0 than Ruby on Rails.
+
==新闻==
 +
{{SeeWikipedia|Django (web framework)}}
 +
*[https://www.djangoproject.com/weblog/2020/aug/04/django-31-released/ Django 3.1 Released] (2020.08.04)
 +
*[https://www.djangoproject.com/weblog/2019/dec/02/django-3-released/ Django 3.0 Released] (2019.12.02)
 +
*[https://www.djangoproject.com/weblog/2017/dec/02/django-20-released/  Django 2.0 released] (2017.12.02)
 +
 
 +
==版本==
 +
===Django 3.x===
 +
[https://www.liujiangblog.com/course/django/ Django3.1 教程]
 +
 
 +
===Django 2.x===
 +
[http://www.b-list.org/weblog/2009/jan/18/django-20/ Django 2.0] is far more friendly for Web 2.0 than Ruby on Rails.
  
 
“Django 2.0” is, apparently, built on the Django 0.96.2 codebase, which is rather interesting since that means it could be missing:
 
“Django 2.0” is, apparently, built on the Django 0.96.2 codebase, which is rather interesting since that means it could be missing:
第41行: 第53行:
 
已经支持Postgresql, MySql, Sqlite3, Oracle, ado_mssql
 
已经支持Postgresql, MySql, Sqlite3, Oracle, ado_mssql
  
==安装指南==
+
==指南==
First, download Django-1.0.tar.gz from http://download.huihoo.com/django/ Then:
+
pip install django
 +
pip install django==1.11.5
 +
pip3 install django==3.1
 +
conda install Django // 通过[[Anaconda python|Anaconda]]的方式安装
 +
python或python3
 +
>>> import django
 +
>>> print(django.get_version())
 +
1.11.5
 +
或者, 下载类似安装包 Django-1.0.tar.gz https://www.djangoproject.com/download/
  
 
  tar xzvf Django-0.95.tar.gz
 
  tar xzvf Django-0.95.tar.gz
第49行: 第69行:
 
  >>> import django
 
  >>> import django
 
  >>> django.VERSION
 
  >>> django.VERSION
 +
>>> from django.template import Template, Context
 +
解决 ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
 +
>>> from django.conf import settings 或 export DJANGO_SETTINGS_MODULE=mysite.settings
 +
>>> settings.configure(DEBUG=False)
 +
>>> <nowiki>t = Template('My name is {{ my_name }}.')</nowiki>
 +
>>> c = Context({'my_name': 'Allen Long'})
 +
>>> t.render(c)
 
  cd /var/www
 
  cd /var/www
 
  django-admin.py startproject mysite
 
  django-admin.py startproject mysite
第58行: 第85行:
 
  可这样启动  #python manage.py runserver 192.168.1.3:8000  
 
  可这样启动  #python manage.py runserver 192.168.1.3:8000  
 
  http://192.168.1.3:8000 // OK 啦
 
  http://192.168.1.3:8000 // OK 啦
 +
 +
==模块化==
 +
Django 模块化、组件、Apps
 +
 +
[https://djangopackages.org/ Django Packages]
 +
* Voting by [[django-voting]]
 +
* Tagging by [[django-tagging]]
 +
* [[DjangoBB]]
 +
*[[django-localdate]]
 +
*[[django-uploadutils]]
 +
*[[django-threadedcomments]]
 +
*[[django-helpers]] -- Rails style helpers for Django
 +
*[[django-tube]] -- Broadcast yourself... with Django.
 +
*[[django-diario]] -- A pluggable weblog;
 +
*[[django-fleshin]] -- Photo management application
 +
*[[django-contact-form]] -- An extensible contact-form application for Django
 +
*[[django-registration]] -- Generic user-registration application;
 +
*[[django-evolution]] -- Schema evolution.
 +
More : http://code.djangoproject.com/wiki/DjangoResources#Djangoapplicationcomponents
 +
 
==MVC==
 
==MVC==
 
models.py (the database tables)
 
models.py (the database tables)
第91行: 第138行:
 
  </ul>
 
  </ul>
 
  </body></html>
 
  </body></html>
==First App==
+
 
 +
==第一个应用==
 
创建一个polls应用
 
创建一个polls应用
 
  mysite/python manage.py startapp polls
 
  mysite/python manage.py startapp polls
第217行: 第265行:
 
  http://localhost:8000/polls/100
 
  http://localhost:8000/polls/100
 
  You're looking at poll 100.
 
  You're looking at poll 100.
==Databases==
+
 
 +
==数据库==
 
  DEFAULT_CHARSET = 'utf-8'
 
  DEFAULT_CHARSET = 'utf-8'
  
第236行: 第285行:
 
  #DATABASE_HOST = 'localhost'
 
  #DATABASE_HOST = 'localhost'
 
  #DATABASE_PORT = '5432'
 
  #DATABASE_PORT = '5432'
==Use Django with Nginx==
+
 
[[Django on Nginx]]
+
===Cassandra===
==use Django with Lighttpd==
+
*[https://pypi.python.org/pypi/django-cassandra-engine/ django-cassandra-engine] - [[Apache Cassandra]] backend for Django
*[[Django on Lighttpd]]
+
 
==use Django with Apache==
+
==Web Server==
 +
===Apache===
 
edit your httpd.conf
 
edit your httpd.conf
 
  <Location "/mysite/">
 
  <Location "/mysite/">
第249行: 第299行:
 
  </Location>  
 
  </Location>  
 
  http://localhost/mysite/
 
  http://localhost/mysite/
==Applications==
 
* Voting by [[django-voting]]
 
* Tagging by [[django-tagging]]
 
*[[django-localdate]]
 
*[[django-uploadutils]]
 
*[[django-threadedcomments]]
 
*[[django-helpers]] -- Rails style helpers for Django
 
*[[django-tube]] -- Broadcast yourself... with Django.
 
*[[django-diario]] -- A pluggable weblog;
 
*[[django-fleshin]] -- Photo management application
 
*[[django-contact-form]] -- An extensible contact-form application for Django
 
*[[django-registration]] -- Generic user-registration application;
 
*[[django-evolution]] -- Schema evolution.
 
More : http://code.djangoproject.com/wiki/DjangoResources#Djangoapplicationcomponents
 
==use Django with FastCGI==
 
  
==成功应用==
+
===Nginx===
 +
[[Django on Nginx]]
 +
 
 +
===Lighttpd===
 +
*[[Django on Lighttpd]]
 +
 
 +
==问题==
 +
收集整理的一些常见问题
 +
*[https://stackoverflow.com/questions/6358030/how-to-reset-django-admin-password How to reset Django admin password?]
 +
* 创建管理员 python manage.py createsuperuser
 +
 
 +
==项目==
 +
[https://github.com/wsvincent/awesome-django Awesome Django] [[image:awesome.png]]
 +
*[http://www.djangosites.org/with-source/ Sites powered by Django with Source Code]
 +
[[Image:djangopowered126x54.gif|right]]
 +
*[[Django CMS]]
 +
*[[Mezzanine]]
 +
*[[Pinax]]
 +
*[[edX]] [https://github.com/edx/edx-platform/tree/master/openedx/core/djangoapps core djangoapps]
 +
*[[django-oscar]]
 +
*[[Taiga]]
 +
*[https://github.com/getsentry/sentry Sentry] application monitoring
 +
*[[Django REST framework]]
 +
*[[Djapian]]
 +
*[[Satchmo]]
 +
*[[dojango]]
 +
*[[Sphene Community Tools]]
 +
*[https://github.com/sshwsfc/xadmin Xadmin]
 +
*[https://github.com/JoneXiong/DjangoX DjangoX]
 +
*[https://github.com/kinegratii/django-echarts django-echarts] 是一个 [http://echarts.baidu.com Echarts] 整合的 Django App,使用 [https://github.com/chenjiandongx/pyecharts pyecharts] 的作为图表构建库。
 +
*[https://github.com/django-nonrel Django and NoSQL]
 +
 
 
===国外===
 
===国外===
 +
*http://mloss.org // open source
 
*http://ellingtoncms.com/
 
*http://ellingtoncms.com/
 
*http://pownce.com/
 
*http://pownce.com/
第288行: 第355行:
 
*[http://worldinpictures.org/ worldinpictures.org] - Enter any worldwide location, see pictures of it (from Flickr).
 
*[http://worldinpictures.org/ worldinpictures.org] - Enter any worldwide location, see pictures of it (from Flickr).
 
*http://lucumr.pocoo.org/
 
*http://lucumr.pocoo.org/
 +
 
===国内===
 
===国内===
 
*http://www.lzpian.com/ (老照片,有点douban的感觉)
 
*http://www.lzpian.com/ (老照片,有点douban的感觉)
 +
*http://www.haokanbu.com/
 
*http://www.ifaxian.com/ (有些特色)
 
*http://www.ifaxian.com/ (有些特色)
 
*http://www.aiyo.cn
 
*http://www.aiyo.cn
 
*http://www.hpbookmarks.com/
 
*http://www.hpbookmarks.com/
==Book==
+
*http://www.aggua.com
*The Django Book: http://books.huihoo.org/django/en/1.0/
+
 
 +
===源代码===
 +
[https://www.djangosites.org/with-source/ Sites with Source Code]
 +
 
 +
==书籍==
 +
*[http://docs.huihoo.com/django/en/2.0/ The Django Book 2.0]
 +
*[http://djangobook.py3k.cn/2.0/ The Django Book 2.0 中文版]
 +
*[http://docs.huihoo.com/django/en/1.0/ The Django Book 1.0]
 +
*[http://masteringdjango.com/ Mastering Django]
 +
 
 
==文档==
 
==文档==
 
*[http://www.ibm.com/developerworks/cn/linux/l-django/ Python Web 框架,第 1 部分: 使用 Django 和 Python 开发 Web 站点]
 
*[http://www.ibm.com/developerworks/cn/linux/l-django/ Python Web 框架,第 1 部分: 使用 Django 和 Python 开发 Web 站点]
 
*[http://www.djangobook.com/ Django Book]: 一本免费的自由文档,This work is licensed under the GNU Free Document License.
 
*[http://www.djangobook.com/ Django Book]: 一本免费的自由文档,This work is licensed under the GNU Free Document License.
==相关链接==
+
*Django Master Class http://toys.jacobian.org/presentations/2007/oscon/tutorial/
*http://www.djangoproject.com/
+
 
*下载 - http://download.huihoo.com/django/
+
==图集==
*文档 - http://docs.huihoo.com/django/
+
<gallery>
 +
image:django-release-roadmap.png|版本路线图
 +
image:Django-middleware.png|中间件
 +
image:django-adminlte2.png|Django AdminLTE2
 +
</gallery>
 +
 
 +
==链接==
 +
*[http://www.djangoproject.com/ Django官网]
 +
*[http://code.djangoproject.com/wiki/DjangoResources Django resources]
 +
*[https://github.com/topics/django Django Topic @ GitHub]
 +
*[http://www.djangosnippets.org/ 编码主要参考]
 +
*[http://djangopackages.com/ Django Packages]
 +
*[http://download.huihoo.com/django/ 下载]
 +
*[http://docs.huihoo.com/django/ Django文档]
 
*http://wiki.woodpecker.org.cn/moin/DjanGo
 
*http://wiki.woodpecker.org.cn/moin/DjanGo
*Django-powered sites - http://www.djangosites.org/ [[Image:djangosite80x15.gif]]
+
*[http://www.djangosites.org/ Django-powered sites] [[Image:djangosite80x15.gif]]
[[Category:Web application frameworks]]
+
*[[Image:djangosearch.gif]] http://djangosearch.com
 +
*[http://segmentfault.com/a/1190000002989928 如何使用 Docker 组件开发 Django 项目?]
 +
*[https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn How To Install and Configure Django with Postgres, Nginx, and Gunicorn]
 +
*[http://blog.kevinastone.com/getting-started-with-django-rest-framework-and-angularjs.html Getting Started with Django Rest Framework and AngularJS]
 +
 
 +
[[category:framework]]
 +
[[category:django]]
 +
[[category:python]]
 +
[[category:huihoo]]

2020年12月22日 (二) 14:27的最后版本

Django.gif

Django: The Web framework for perfectionists with deadlines

目录

[编辑] 简介

Django 是一个高级 Python web framework,它鼓励快速开发和干净的、MVC设计。它包括一个模板系统,对象相关的映射和用于动态创建管理界面的框架。Django遵守BSD版权。

Django 很像 Ruby on Rails.

没有真理,只有解释。—— 尼采

[编辑] 新闻

Wikipedia-35x35.png 您可以在Wikipedia上了解到此条目的英文信息 Django Thanks, Wikipedia.

[编辑] 版本

[编辑] Django 3.x

Django3.1 教程

[编辑] Django 2.x

Django 2.0 is far more friendly for Web 2.0 than Ruby on Rails.

“Django 2.0” is, apparently, built on the Django 0.96.2 codebase, which is rather interesting since that means it could be missing:

  • At least one criticial security fix (that’s why we had a Django 0.96.3).
  • The refactored Django ORM, including much more flexible query backends and support for model inheritance (queryset-refactor happened after 0.96).
  • Full Unicode support (the Unicode branch happened after 0.96).
  • Auto-escaping templates, so open wide for cross-site scripting attacks (auto-escaping landed after 0.96).
  • The full django.forms library, including formsets and easy model-generated forms and support for generic relations (all landed after 0.96).
  • Swappable uploading and file-storage handlers (landed after 0.96).
  • The easier-to-use, easier-to-customize, much-more-flexible admin (newforms-admin landed after 0.96).
  • django.contrib.gis for GIS support (landed after 0.96).
  • The refactored contrib.comments (landed after 0.96).
  • The refactored (and immensely faster) signal dispatcher (landed after 0.96).
  • The ability to run Django on the Java virtual machine (Jython compatibility didn’t happen until 1.0).
  • An order-of-magnitude faster test framework (landed last week for Django 1.1).
  • Aggregation support in the ORM (landed last week for Django 1.1).
  • A couple thousand bugfixes which have gone into Django since 0.96.
  • Somewhere around 40,000 lines of added/improved documentation.

[编辑] 特性

  • 对象相关的映射

完全在Python中定义你的数据模型。你可以免费得到一个丰富的,动态访问数据库的API--但如果需要你仍然可以写SQL语句。

  • URL 分发

URL的设计漂亮,cruft-free,没有框架的特定限定。象你喜欢的一样灵活。

  • 模版系统

使用DjanGo强大而可扩展的模板语言来分隔设计、内容和Python代码。

  • Cache系统

可以挂在内存缓冲或其它的框架实现超级缓冲 -- 实现你所需要的粒度。

  • 自动化的管理界面

不需要你花大量的工作来创建人员管理和更新内容的接口。DjanGo可以自动完成。

  • 支持多种数据库

已经支持Postgresql, MySql, Sqlite3, Oracle, ado_mssql

[编辑] 指南

pip install django
pip install django==1.11.5
pip3 install django==3.1
conda install Django // 通过Anaconda的方式安装
python或python3 
>>> import django
>>> print(django.get_version())
1.11.5

或者, 下载类似安装包 Django-1.0.tar.gz https://www.djangoproject.com/download/

tar xzvf Django-0.95.tar.gz
cd Django-0.95
sudo python setup.py install // 会去下载相关 egg 包,egg与Java jar 类似,包含版本化的包和依赖性管理
>>> import django
>>> django.VERSION
>>> from django.template import Template, Context
解决 ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
>>> from django.conf import settings 或 export DJANGO_SETTINGS_MODULE=mysite.settings
>>> settings.configure(DEBUG=False)
>>> t = Template('My name is {{ my_name }}.')
>>> c = Context({'my_name': 'Allen Long'})
>>> t.render(c)
cd /var/www
django-admin.py startproject mysite
cd mysite
python manage.py runserver
http://localhost:8000
or http://192.168.1.3:8000 //本机 IP
Alert!: Unable to connect to remote host
可这样启动  #python manage.py runserver 192.168.1.3:8000 
http://192.168.1.3:8000 // OK 啦

[编辑] 模块化

Django 模块化、组件、Apps

Django Packages

More : http://code.djangoproject.com/wiki/DjangoResources#Djangoapplicationcomponents

[编辑] MVC

models.py (the database tables)

from django.db import models
class Book(models.Model):
    name = models.CharField(maxlength=50)
    pub_date = models.DateField()

views.py (the business logic)

from django.shortcuts import render_to_response
from models import Book
def latest_books(request):
    book_list = Book.objects.order_by('-pub_date')[:10]
    return render_to_response('latest_books.html', {'book_list': book_list})

urls.py (the URL configuration)

from django.conf.urls.defaults import *
import views
urlpatterns = patterns(,
    (r'latest/$', views.latest_books),
)

latest_books.html (the template)

<html><head><title>Books</title></head>
<body>
Books
    {% for book in book_list %} <li>{{ book.name }}</li> {% endfor %}
</body></html>

[编辑] 第一个应用

创建一个polls应用

mysite/python manage.py startapp polls
vi polls/models.py
from django.db import models
class Poll(models.Model):
   question = models.CharField(maxlength=200)
   pub_date = models.DateTimeField('date published')
class Choice(models.Model):
   poll = models.ForeignKey(Poll)
   choice = models.CharField(maxlength=200)
   votes = models.IntegerField()
vi settings.py
INSTALLED_APPS = (
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'mysite.polls'  // 增加这一行
)

创建数据库

mysql>create database polls
vi settings.py
DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'polls'
DATABASE_USER = 'root'
DATABASE_PASSWORD = 
DATABASE_HOST = 'localhost'
DATABASE_PORT = '3306'
python manage.py syncdb // 修改表结构后,可用此步骤更新数据库

python manage.py sql polls
BEGIN;
CREATE TABLE `polls_poll` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `question` varchar(200) NOT NULL,
    `pub_date` datetime NOT NULL
);
CREATE TABLE `polls_choice` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `poll_id` integer NOT NULL REFERENCES `polls_poll` (`id`),
    `choice` varchar(200) NOT NULL,
    `votes` integer NOT NULL
);
COMMIT;

可看到在mysql中已经创建了相关表

mysql> show tables;
+----------------------------+
| Tables_in_poll             |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_message               |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_content_type        |
| django_session             |
| django_site                |
| polls_choice               |
| polls_poll                 |
+----------------------------+
12 rows in set (0.00 sec)
python manage.py shell
>>> from mysite.polls.models import Poll, Choice
>>> Poll.objects.all()
[]
>>> from datetime import datetime
>>> p = Poll(question="What's up?", pub_date=datetime.now())
>>> p.save()
>>> p.id
1L
>>> p.question
"What's up?"
>>> p.pub_date
datetime.datetime(2006, 9, 1, 15, 8, 1, 135114)

Django admin site默认没有激活,可通过以下三个步骤激活它

1. Add "django.contrib.admin" to your INSTALLED_APPS setting in settings.py
2. Run python manage.py syncdb // Creating table django_admin_log
3. vi urls.py  
 (r'^admin/', include('django.contrib.admin.urls')),
python manage.py runserver
http://local:8000/admin/ :)
vi mysite/polls/models.py
class Polls(models.Model):
   # ...
   class Admin:
       pass
vi mysite/urls.py

from django.conf.urls.defaults import *
urlpatterns = patterns(,
    (r'^polls/$', 'mysite.polls.views.index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
)
vi mysite/polls/views.py

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")
def detail(request, poll_id):
   return HttpResponse("You're looking at poll %s." % poll_id)
http://localhost:8000/polls/
Hello, world. You're at the poll index.
http://localhost:8000/polls/100
You're looking at poll 100.

[编辑] 数据库

DEFAULT_CHARSET = 'utf-8'
#DATABASE_ENGINE = 'sqlite3'
#DATABASE_NAME = os.path.join(DIRNAME, 'database.db')
DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'tagging_test'
DATABASE_USER = 'root'
DATABASE_PASSWORD = 
DATABASE_HOST = 'localhost'
DATABASE_PORT = '3306'
#DATABASE_ENGINE = 'postgresql_psycopg2'
#DATABASE_NAME = 'tagging_test'
#DATABASE_USER = 'postgres'
#DATABASE_PASSWORD = 
#DATABASE_HOST = 'localhost'
#DATABASE_PORT = '5432'

[编辑] Cassandra

[编辑] Web Server

[编辑] Apache

edit your httpd.conf

<Location "/mysite/">
   SetHandler python-program
   PythonHandler django.core.handlers.modpython
   SetEnv DJANGO_SETTINGS_MODULE mysite.settings
   PythonDebug On
</Location> 
http://localhost/mysite/

[编辑] Nginx

Django on Nginx

[编辑] Lighttpd

[编辑] 问题

收集整理的一些常见问题

[编辑] 项目

Awesome Django Awesome.png

Djangopowered126x54.gif

[编辑] 国外

[编辑] 国内

[编辑] 源代码

Sites with Source Code

[编辑] 书籍

[编辑] 文档

[编辑] 图集

[编辑] 链接

分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱