標籤:
New Project
1.建立 django-admin startproject mysite
2.運行 manage.py runserver 8080
New APP
1.manage.py startapp polls
2.write view.py: def index 等等
3.create urls.py: urlpatterns=[ url() ]
4.url連結到project中:修改添加mysite/urls.py,
url(r‘^polls/‘, include(‘polls.urls‘)),
5.安裝資料庫 manage.py migrate
6.creating models
7.Activating models 在mysite/settings.py 中添加一行
8建立編譯 manage.py makemigrationgs polls
修改編譯manage.py migrate
9運作,輸入資料
manage.py shell
import django
django.setup()
-------------
from polls.models import Question,Choice
Question.objects.all()
from django.utils import timezone
q = Question(question_text="What‘s new?", pub_date=timezone.now())
More Vies
1.vies.py 新增 def detail results vote
2.polls/urls.py新增url
添加模板Template
1.在APP的檔案下建立template/app/index.html
2.編輯html檔案
3.更新polls/views.py
New 2 APPS
1.manage.py startapp polls2,在project中添加註冊
2.copy polls 的templates,建立檔案夾修改app name
3.copy修改models,
4.修改urls.
5.修改views
主要所有polls修改為polls2
urls.py中的app_name
views.py中的好多
還有xxx.html中的
Make the poll app modifiable in the admin?
polls2/admim.py 中添加model
from django.contrib import adminfrom .models import Questionadmin.site.register(Question)
ListView and DetailView
We’re using two generic views here: ListView and DetailView. Respectively, those two views abstract the concepts of “display a list of objects” and “display a detail page for a particular type of object.”
通用視圖
除了object_list外,Django還提供了許多通用視圖函數,分布在幾個模組中:django.views.generic.list_detail模組
- object_list 顯示模型對象列表
- object_detail 顯示單個模型對象
django.views.generic.create_update模組
- create_object 建立模型對象
- update_object 修改模型對象
- delete_object 刪除模型對象
django.views.generic.simple模組
- direct_to_template 直接使用指定的模板渲染給定的context對象
- redirect_to 重新導向到指定的url
django.views.generic.date_based模組
這個模組主要處理“按時間查看存檔”的功能,來源於新聞出版行業。具體包括:
- archive_index 最頂級的歸檔,列出所有年份及指定數量的最新對象
- archive_year 按年歸檔,列出所有擁有對象的月份
- archive_month 按月歸檔,列出本月的所有對象,找到擁有對象的上一個、下一個月份
- archive_week 按周歸檔,列出本周的所有對象
- archive_day 按日歸檔,列出當天的所有對象,找到擁有對象的上一個、下一個日期
- archive_today 當前日期(今天)的按日歸檔
- object_detail 顯示按照年/月/日/序號找到的對象
Django~NewProject and APP