django手册

安装:
pip install Django==1.7.2
pip install Django==1.6.2    // BAE支持版本
验证安装版本:
>>> import django
>>> print(django.get_version())

启动server服务:
python manage.py runserver


1. 创建新工程
django-admin.py startproject mysite  // django-admin.py请在django的安装目录内查找
新工程的目录结构:
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

2. 创建应用
python manage.py startapp polls
新应用的目录结构:
polls/
    __init__.py
    admin.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

3. 将新应用加入工程, mysite/settings.py
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

3. 创建应用的后台数据
3.1 Database设置, mysite/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'mysitedb.sqlite3'),
    }
}

// 如果使用Mysql,需要先在Mysql中创建相应的数据库,才能连接成功
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysitedb.mysql',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '3306',  // Mysql默认本地数据库服务port
    }
}

3.2 编写应用的后台数据结构, polls/models.py

3.3 检查models.py是否正确:
      python manage.py validate 

3.4 查看models.py对应的sql语句:
      python manage.py sql polls                           // 1.6版本
      python manage.py sqlall polls                       // 1.6版本
      python manage.py sqlmigrate polls 0001     // 1.7版本

3.5 创建数据库文件,根据models.py内容创建数据库的table:
     python manage.py migrate  // 1.7版本
     python manage.py syncdb    // 1.6版本
The  migrate  command looks at the  INSTALLED_APPS  setting and creates any necessary database tables according to the database settings in your  mysite/settings.py  file and the database migrations shipped with the app . 

3.6 更新数据库文件,models.py内容更新之后,相应的对数据库的table进行更新:
     python manage.py makemigrations polls  // 1.7版本
     python manage.py syncdb    // 1.6版本

By running makemigrations, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration.

Migrations are how Django stores changes to your models (and thus your database schema) - they’re just files on disk. 

4. 创建views, polls/views.py
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

5. 建立Http访问URL和view的映射, polls/urls.py
from django.conf.urls import patterns, url
from polls import views

urlpatterns = patterns('',
    # ex: /polls/
    url(r'^$', views.index, name='index'),
    # ex: /polls/5/
    url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
)

6. 建立网站入口URL到应用的映射, mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
     url(r'^polls/', include('polls.urls')),  // host之后“polls”对应的URL就被定位到'polls.urls'中去
    url(r'^admin/', include(admin.site.urls)),
)

7. 在Admin中编辑数据库, polls/admin.py
from django.contrib import admin

# Register your models here.
from polls.models import Choice, Question

class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Level',            {'fields': ['question_level']}),
    ]
    list_display = ('question_text', 'pub_date')

admin.site.register(Question, QuestionAdmin)

8. 使用Template
8.1  新建Template:
     polls/templates/polls/index.html
     polls/templates/polls/detail.html

8.2 使用generic view创建view, polls/views.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic

from polls.models import Choice, Question

class IndexView(generic.ListView):
     template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]


class DetailView(generic.DetailView):
    model = Question
     template_name = 'polls/detail.html'

8.3 修改URL到view的映射, polls/urls.py
from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$',  views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>\d+)/$',  views.DetailView.as_view(), name='detail'),
)

9. http使用css
9.1 新建css:
     polls/static/polls/style.css
9.2 在html中使用css, polls/templates/polls/index.html
{% load staticfiles %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

10. 使用图片
10.1 新建图片:
      polls/static/polls/images/background.gif

10.2 在css中使用, polls/static/polls/style.css:
body {
    background: white url(" images/background.gif") no-repeat right bottom;
}















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值