构建个人博客网站
1、简单构建
2、初步创建blog应用
博文+博客分类
创建项目文件
django-admin startproject mysite
创建APP(记得加在settings.py里面)
cd mysite
python manage.py startapp blog
定义models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class BlogType(models.Model):
type_name = models.CharField(max_length=15)
def __str__(self):
return self.type_name
class Blog(models.Model):
title = models.CharField(max_length=50)
blog_type = models.ForeignKey(BlogType,on_delete=models.DO_NOTHING)
content = models.TextField()
author = models.ForeignKey(User,on_delete=models.DO_NOTHING)
create_time = models.DateTimeField(auto_now_add=True)
last_updated_time = models.DateTimeField(auto_now=True)
def __str__(self):
return "<Blog:%s>" % self.title
初始化数据库,然后创建超级用户
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
为了方便查看,我们可以改一下admin.py
from django.contrib import admin
from .models import BlogType,Blog
# Register your models here.
@admin.register(BlogType)
class BlogTypeAdmin(admin.ModelAdmin):
list_display = ('id','type_name')
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
list_display = ('title','blog_type','author','created_time','last_updated_time')
进入后台管理界面,添加数据