1.新建blog项目:
创建app01分项:
添加urls.py文件
from app01 import views
注册app01 , path('app01/' include('app01.urls'))
添加static文件夹并导入前端文件,在外部也新建static文件夹
settings文件中添加:STATICFILES_DIRS=[os.path.join(BASE_DIR,'staitc')]
添加templates文件夹并导入前端html文件
编写app01中views.py文件:
pass 后续再写
修改前端html文件:
创建模板文件 , 修改其余文件代码
修改连接视图函数的相关代码
创建app02分项:
开始同app01相关操作
添加templates-->app02文件夹用于添加后台管理博客系统,添加作者与文章
article.html
author.html
index.html
创建模型类:
# 作者类
class Author(models.Model):
name = models.CharField(max_length=32)
description = models.TextField()
def __str__(self):
return '<obj name:{}>'.format(slef.name)
# 图片类
class Image(models.Model):
photo = models.CharField(max_length=32)
author = models.ForeignKey(to='Author',on_delete=models.CASCADE)
# 文章类
class Article(models.Model):
title = models.CharField(max_length=32)
description = models.CharField(max_length=32)
content = models.TextField()
date = models.CharField(max_length=32)
picture = models.CharField(max_length=32)
author = models.ForeignKey(to='Author',on_delete=models.CASCADE)
def __str__(self):
return '<obj title:{}'.format(self.title)
编写视图函数:
def index(request):
return render(request,'app02/index.html')
def author(request):
if request.method == 'POST':
name = request.POST.get('name')
desc = request.POST.get('desc')
# 创建作者
author_obj = models.Author.objects.create(
name = name
description = desc
)
# 上传个人相册
photo_obj_list = request.FILES.getlist('photo')
for photo_obj in photo_obj_list:
# 在外部新建static的文件夹中添加upload-->images用于存放用户提交的图片
path = 'static/upload/images'+ photo_obj.name
with open(path ,mode='wb') as f:
for con in photo_obj.chunks():
f.write(con)
image_obj = models.Image()
image_obj.photo = 'upload/images' + photo_obj.name
image_obj.author = author_obj
image_obj.save()
return HttpResponse('ok')
return render(request,'app02/author.html')
def article(request):
if request.method == 'POST':
# 1.获取数据
title = request.POST.get('title')
desc = request.POST.get('desc')
content = request.POST.get('content')
author_id = request.POST.get('author_id')
date = request.POST.get('date')
pic_obj = request.FILES.get('pic')
# 2.保存图片
path = 'static/upload/images'+pic_obj.name
with open(path,mode='wb') as f:
for con in pic_obj.chunks():
f.write(con)
# 3.保存数据
article_obj = models.Article()
article_obj.title = title
article_obj.description = desc
article_obj.content = content
article_obj.date = date
article_obj.author_id = author_id
article_obj.picture = 'upload/images' + pic_obj.name
article_obj.save()
return HttpResponse('ok')
else:
article_obj = models.Article.objects.all()
return render(request ,'app02/article.html',locals())
配置urls
回到app01中, 将app02中编写的内容引入并替换掉app01中原静态文件,将其动态化
编写app01中views.py文件:
def index(request):
return render(request):
return render(request,'app01/index.html')
# 个人简介
def grjj(request):
author_obj = models.Author.objects.first()
image_obj = models.Image.object.first()
path = image_obj.photo
return render(request,'app01/about.html',locals())
# 个人相册
def grxc(request):
author_obj = models.Author.objects.first()
image_obj_list = models.Image.objects.filter(author=author_obj)
return render(request,'app01/listpic.html',locals())
# 个人日记
from django.core.paginator import Paginator,EmptyPage
def grrj(request):
article_obj_list = models.Article.objects.all()
# 分页
my_num = 2
paginator = Paginator(article_obj_list,my_num) # 分两页显示
num_pages = paginator.num_pages
count = paginator.count
try:
current_num = int(request.GET.get('page',1))
page_obj = paginator.page(current_num) # 获取当前页
except EmptyPage:
page_obj = paginator.page(1)
return render(request,'app01/newslistpic.html',locals())
# 展示文章具体内容
def show_rj(request):
id = request.GET.get('id')
article = models.Article.objects.get(id=id)
return render(request,'app01/show_rj',locals())
修改html文件中相关连接的路由