Django個人博客搭建3-創建superuser並向數據庫中添加數據並改寫視圖

Django個人博客搭建1-創建Django項目和第一個App
Django個人博客搭建2-編寫文章Model模型,View視圖
Django個人博客搭建3-創建superuser並向數據庫中添加數據並改寫視圖
Django個人博客搭建4-配置使用 Bootstrap 4 改寫模板文件
Django個人博客搭建5-編寫文章詳情頁面並支持markdown語法
Django個人博客搭建6-對文章進行增刪查改
Django個人博客搭建7-對用戶登陸註冊等需求的實現
Django個人博客搭建8-優化文章模塊
Django個人博客搭建9-增加文章評論模塊
1.創建superuser

上章說到瀏覽器已經打印出hello world了
我們需要後臺擁有數據,不過不需要我們手動向數據庫添加,django內置了一個後臺管理工具
在控制檯輸入如下代碼創建superuser

python manage.py createsuperuser

依次輸入用戶名密碼,郵箱什麼的可以不填:

F:\PycharmProject\myblog\myblog>python manage.py createsuperuser
System check identified some issues:

WARNINGS:
article.ArticlePost.created: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Username (leave blank to use 'mechrevo'): root
Email address:
Password:
Password (again):
Superuser created successfully.

F:\PycharmProject\myblog\myblog>

可以看見成功創建了superuser

2. 將ArticlePost註冊到後臺中
打開article/admin.py,寫入以下代碼:

from django.contrib import admin

# Register your models here.

from django.contrib import admin

from .models import ArticlePost

# 註冊ArticlePost到admin中
admin.site.register(ArticlePost)

這樣後臺就註冊好了

3. 熟悉django後臺並添加數據
在創建完superuser後我們可以在瀏覽器中輸入 http://127.0.0.1:8000/admin/ 後看到如下界面
在這裏插入圖片描述
輸入用戶名密碼後進入後臺
在這裏插入圖片描述
可以看到網站界面是英文的,可以在settings.py中進行中文設置:
修改
LANGUAGE_CODETIME_ZONE

LANGUAGE_CODE = 'zh-Hans'

TIME_ZONE = 'Asia/Shanghai'

網站界面就變成中文的了,並且時間用的是上海時間.紅框內就是剛剛添加的數據表
在這裏插入圖片描述
點擊進入後點擊右上角的增加按鈕進行數據的添加
我們可以添加多條數據,這裏我添加了三條
在這裏插入圖片描述
打開用navicat打開數據庫可以看到
在這裏插入圖片描述
數據庫中增加了三條數據

4. 改寫視圖函數
改寫article/views.py中的article_list函數

from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse
from .models import ArticlePost


# 視圖函數
def article_list(request):
    # 取出所有博客文章
    articles = ArticlePost.objects.all()
    # 需要傳遞給模板對象
    context = {'articles': articles}
    # render函數載入模板,並返回context對象
    return render(request, 'article/list.html', context)
    # article/list.html:模板位置          context:傳入模板的對象

render中: article/list.html代表模板位置 context:傳入模板的對象

5. 編寫模板
在根目錄下創建模板文件夾templates,在templates中再新建article文件夾代表是article app的模板,再創建一個html文件list.html

在這裏插入圖片描述
並且需要告訴django模板的位置,修改 settings.py中的TEMPLATES下的DIRS如下:

TEMPLATES = [
   {
       'BACKEND': 'django.template.backends.django.DjangoTemplates',
       'DIRS': [os.path.join(BASE_DIR, 'templates')],
       'APP_DIRS': True,
       'OPTIONS': {
           'context_processors': [
               'django.template.context_processors.debug',
               'django.template.context_processors.request',
               'django.contrib.auth.context_processors.auth',
               'django.contrib.messages.context_processors.messages',
           ],
       },
   },
]

並在list.html中添加如下代碼:

{% for article in articles %}
   <p>{{ article.title }}</p>
{% endfor %}

用瀏覽器打開 http://127.0.0.1:8000/article/article-list/ 可以看到成功顯示了三篇文章
在這裏插入圖片描述

Django個人博客搭建1-創建Django項目和第一個App
Django個人博客搭建2-編寫文章Model模型,View視圖
Django個人博客搭建3-創建superuser並向數據庫中添加數據並改寫視圖
Django個人博客搭建4-配置使用 Bootstrap 4 改寫模板文件
Django個人博客搭建5-編寫文章詳情頁面並支持markdown語法
Django個人博客搭建6-對文章進行增刪查改
Django個人博客搭建7-對用戶登陸註冊等需求的實現
Django個人博客搭建8-優化文章模塊
Django個人博客搭建9-增加文章評論模塊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章