-
通過排序來選擇最新的五篇文章
top5_article_list?=?Article.objects.order_by('-article_id')[:5] paginator?=?Paginator(all_article,?3) page_num?=?paginator.num_pages page_article_list?=?paginator.page(page) if?page_article_list.has_next(): ????next_page?=?page?+?1 else: ????next_page?=?page if?page_article_list.has_previous(): ????previous_page?=?page?-?1 else: ????previous_page?=?page return?render(request,?'blog/index.html',?{ ????'article_list':?page_article_list, ????'page_num':?range(1,?int(page_num)?+?1), ????'curr_page':?page, ????'previous_page':?previous_page, ????'next_page':?next_page, ????'top5_article_list':?top5_article_list })
查看全部 -
實現(xiàn)分頁按鈕
設(shè)計?page= 的url
def?get_index_page(request): ????page?=?request.GET.get('page') ????if?page: ????????page?=?int(page) ????else: ????????page?=?1 ????all_article?=?Article.objects.all() ????paginator?=?Paginator(all_article,?3) ????page_num?=?paginator.num_pages ????page_article_list?=?paginator.page(page) ????if?page_article_list.has_next(): ????????next_page?=?page?+?1 ????else: ????????next_page?=?page ????if?page_article_list.has_previous(): ????????previous_page?=?page?-?1 ????else: ????????previous_page?=?page ????return?render(request,?'blog/index.html',?{ ????????'article_list':?page_article_list, ????????'page_num':?range(1,?int(page_num)?+?1), ????????'curr_page':?page, ????????'previous_page':?previous_page, ????????'next_page':?next_page ????})
查看全部 -
在文章詳情頁增加前后篇文章的跳轉(zhuǎn)按鈕
html改造
def?get_detail_page(request,?article_id): ????curr_article?=?None ????previous_article?=?None ????next_article?=?None ????previous_index?=?0 ????next_index?=?0 ????all_article?=?Article.objects.all() ????for?index,?article?in?enumerate(all_article): ????????if?index?==?0: ????????????previous_index?=?0 ????????????next_index?=?index?+?1 ????????elif?index?==?len(all_article)?-?1: ????????????previous_index?=?index?-?1 ????????????next_index?=?index ????????else: ????????????previous_index?=?index?-?1 ????????????next_index?=?index?+?1 ????????if?article.article_id?==?article_id: ????????????curr_article?=?article ????????????previous_article?=?all_article[previous_index] ????????????next_article?=?all_article[next_index] ????????????break ????return?render(request,?'blog/detail.html',?{ ????????'curr_article':?curr_article, ????????'previous_article':?previous_article, ????????'next_article':?next_article ????})
查看全部 -
使用url來對指定文章的詳情頁進行訪問
html加入a標簽
是點擊可以跳轉(zhuǎn)鏈接
查看全部 -
使用模板系統(tǒng)實時的進行渲染
將數(shù)據(jù)通過
return?render(request,?'blog/detail.html',?{ ????'curr_article':?curr_article })
返回給頁面進行顯示
查看全部 -
django的模板系統(tǒng)
網(wǎng)頁邏輯和網(wǎng)頁視圖應(yīng)該分開設(shè)計
{{變量}}
?{%for x in list %},{% endfor %}
?{% if %},{% else %},?{% endif %}
查看全部 -
使用bootstrap實現(xiàn)靜態(tài)博客頁面
簡單易上手
柵格系統(tǒng)
將頁面分為12份
查看全部 -
使用django的模板來渲染頁面
進一步完成博客界面
查看全部 -
def?article_content(request): ????article?=?Article.objects.all()[0] ????title?=?article.title ????brief_content?=?article.brief_content ????content?=?article.content ????article_id?=?article.article_id ????publish_date?=?article.publish_date ????return_str?=?'title:%s,brief_content:%s,content:%s,article_id:%s,publish_date:%s'?%?(title,?brief_content,?content, ?????????????????????????????????????????????????????????????????????????????????????????article_id,?publish_date) ????return?HttpResponse(return_str)
返回博客內(nèi)容
查看全部 -
django admin是自帶的后臺管理工具
自己在admin中注冊模板
from?.models?import?Article admin.site.register(Article)
讓后臺管理顯示標題分辨不同文章
def?__str__(self): ????return?self.title
查看全部 -
django shell
可以用于小范圍的debug
python manage.py shell
from blog.models import Article
a=Article()
a.title='Test shell'
a.brief_content='hello shell'
a.content='hello world hello django hello shell'
a.save()
print(Article.objects.all()[0].title)
查看全部 -
博客包括
標題
摘要
內(nèi)容
IntegerField
TextField
DateTimeField
AutoField自增
primary_key主鍵
創(chuàng)建模型
class?Article(models.Model): ????article_id=models.AutoField(primary_key=True) ????title=models.TextField() ????brief_content=models.TextField() ????content=models.TextField() ????publish_date=models.DateTimeField(auto_now=True)
命令生成遷移文件
python manage.py makemigrations
命令導(dǎo)入文件
python manage.py migrate
查看全部 -
視圖層與數(shù)據(jù)庫中間是模型層
python與數(shù)據(jù)庫表之間轉(zhuǎn)換
屏蔽數(shù)據(jù)庫之間的差異
專注于業(yè)務(wù)邏輯的開發(fā)
查看全部 -
模型層的定義
如何創(chuàng)建一個文章的模型層
了解django shell
django admin模塊了解
查看全部 -
from?django.http?import?HttpResponse #?Create?your?views?here. def?hello_world(request): ????return?HttpResponse('hello?world')
urlpatterns=[ ????path('hello_world',blog.views.hello_world) ]
完成項目路由,和應(yīng)用路由對視圖函數(shù)的綁定
查看全部
舉報