博客系統:
1 表關係
2 登陸驗證(基於ajax與用戶認證組件)
3 註冊 (基於ajax與forms組件)
4 首頁文章列表
5 我的站點頁面設計
6 點贊與踩滅
7 評論
一
(1)
FileField與ImageField
models.py
class userinfo:
avatar = models.FileField(upload_to='avatars/', default="/avatars/default.png")
views.py
avatar=request.FILES.get("avatar_img")
UserInfo.objects.create_user(username=user,password=pwd,email=email,avatar=avatar)
Django會在項目的根目錄建立avatars文件夾,將上傳文件下載到該文件夾中,avatar字段保存的是文件的相對路徑。
(2)設置上傳文件存儲文件夾
if 配置 MEDIA_ROOT=os.path.join(BASE_DIR,"blog","media")
Django會MEDIA_ROOT下建立avatars文件夾,將上傳文件下載到該文件夾中,avatar字段保存的是文件的相對路徑。
(3)配置用戶上傳文件可訪問
url.py
# media 配置
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
settings.py
MEDIA_URL="/media/"
瀏覽器能夠直接訪問http://127.0.0.1:8000/media/avatars/lufei.jpg,即咱們的用戶上傳文件
二
admin:對模型表進行增刪改查
admin.py
admin.site.register(UserInfo)
http://127.0.0.1:8000/admin/blog/category/
http://127.0.0.1:8000/admin/blog/category/add/
http://127.0.0.1:8000/admin/blog/category/4/change/
http://127.0.0.1:8000/admin/blog/category/4/delete/
三
表與表之間的關係:一對多,多對多,一對一
book
ID title publish
1 go 蘋果出版社
2 python 蘋果出版社
3 linux 香蕉出版社
book
ID title publish_id
1 go 1
2 python 1
3 linux 1
Publish
ID name email addr charcher
1 蘋果出版社 123 beijing egon
一旦肯定是一對多的關係,就得建立關聯字段
publish_id
Foreignkey publish_id Refference Publish(ID)
#################################################################
四 分組
1-------
book
ID title publish
1 go 蘋果出版社
2 python 蘋果出版社
3 linux 香蕉出版社
select count(*),publish from Book group by publish
2-------
book
ID title publish_id
1 go 1
2 python 1
3 linux 2
Publish
ID name email addr charcher
1 蘋果出版社 123 beijing egon
2 橘子出版社 123 beijing alex
# 查詢每個出版社對應的書籍個數
select count("title") from Book group by publish_id
# 查詢每個出版社的名字以及對應的書籍個數
select * from book left join publish on book.publish_id=Publish.ID
ID title publish_id ID name email addr charcher
1 go 1 1 蘋果出版社 123 beijing egon
2 python 1 1 蘋果出版社 123 beijing egon
3 linux 2 2 橘子出版社 123 beijing alex
select Publish.name,count(*) from book left join publish on book.publish_id=Publish.ID group by Publish.ID,Publish.name
ORM分組查詢:(annotate():按annotate前面的select字段進行group by)(關鍵詞:每個)
多表:
Publish.objects.all().annotate(c=Count(book)).values("name","c")
Book.objects.all().annotate(c=Count(authors)).values("title","c")
Author.objects.all().annotate(x=Max(book__prcie))
單表:
Book.objects.all().values("publish").annotate(Count("title"))
'''
1.
ret=Article.objects.all().values("user").annotate(c=Count("title")).values("user_id","c")
SELECT "blog_article"."user_id", COUNT("blog_article"."title") AS "c" FROM "blog_article" GROUP BY "blog_article"."user_id"
2.
ret=Category.objects.filter(blog=blog).annotate(c=Count("article")).values("title","c")
SELECT "blog_category"."title", COUNT("blog_article"."nid") AS "c"
FROM "blog_category" LEFT OUTER JOIN "blog_article"
ON ("blog_category"."nid" = "blog_article"."category_id")
WHERE "blog_category"."blog_id" = 1
GROUP BY "blog_category"."nid", "blog_category"."title", "blog_category"."blog_id"
'''
#####################################################################
文章日期歸檔
1
MySQL: date_format
sqlite: striftime
2 extra
models.Article.objects
.extra(select={"standard_time":"strftime('%%Y-%%m-%%d',create_time)"})
.values("standard_time","nid","title")
3 ORM分組查詢(單表)