urls:
from django.contrib import admin
from django.urls import path, re_path, include
from novel import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('login/', views.login),
path('add_novel/', views.add_novel),
path('show_novel/
path('show_own_novel/', views.show_own_novel),
path('del_novel/
re_path(r'^search/', include('haystack.urls')),
]
setting:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
models:
from django.db import models
# Create your models here.
class Author(models.Model):
# id
author_name = models.CharField(max_length=30)
password = models.CharField(max_length=30)
class Novel(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
# 发布时间
publish_time = models.DateField(auto_now_add=True)
# 小说的图片路径
pic = models.CharField(max_length=100)
# 小说的简述
resume = models.CharField(max_length=100)
# 小说名称
name = models.CharField(max_length=50)
views:
from django.shortcuts import render, redirect, HttpResponse
from novel import models
from Django_Novel import settings
import os
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# Create your views here.
# 登录
def login(request):
if request.method == 'GET':
return render(request, 'login.html')
if request.method == 'POST':
name = request.POST.get('name')
# 张嘉佳 张佳
pwd = request.POST.get('password')
if all([name, pwd]):
author = models.Author.objects.get(author_name=name)
# 根据name查出信息数据
if author.password == pwd:
request.session['author'] = author.id
return redirect('/show_own_novel/')
else:
return redirect('/login/')
return render(request,'login.html')
# 添加小说
def add_novel(request):
if request.method == 'GET':
return render(request, 'add_novel.html')
if request.method == 'POST':
author_ = request.session.get('author')
# 获取session中储存的作者ID
name = request.POST.get('name')
resume = request.POST.get('resume') # 简述
file = request.FILES.get('pic')
if file:
base_path = settings.STATICFILES_DIRS[0] # 拿到当前的静态目录 static
file_path = os.path.join(base_path, 'img/' + file.name)
with open(file_path, 'wb') as fp: # 本次存储的位置
if file.multiple_chunks: # 查看文件是否大于2.5M
for buf in file.chunks(): # for循环遍历迭代器,读取存储文件内容 IO流
fp.write(buf)
else:
fp.write(file.read()) # 小文件直接全部写入,并且不需要生成迭代器
models.Novel.objects.create(
name=name,
resume=resume,
pic='img/' + file.name,
author_id=author_,
)
return render(request, 'add_novel.html')
# return HttpResponse('成功')
# 小说展示
def show_novel(request, id):
all = models.Novel.objects.filter(pk=id).all()
return render(request, 'show_novel.html', locals())
# 展示自己的小说
def show_own_novel(request):
author_ = request.session.get('author')
zuozhe = models.Author.objects.get(pk=author_)
# own_novel = models.Novel.objects.filter(author=author_).all()
own_novel = zuozhe.novel_set.all()
return render(request, 'show_own_novel.html', locals())
# 删除小说
def del_novel(request, id):
# 传递了小说的ID,通过ID把具体的小说查出来,将小说删除
novel = models.Novel.objects.filter(pk=id).delete()
# novel.objects.update(author=None)
author_ = request.session.get('author')
zuozhe = models.Author.objects.get(pk=author_)
# own_novel = models.Novel.objects.filter(author=author_).all()
own_novel = zuozhe.novel_set.all()
return render(request,'show_own_novel.html', locals())
# 首页分页展示
def index(request):
all = models.Novel.objects.all().order_by('publish_time')
p = Paginator(all, 3)
page_id = request.GET.get('page_id')
if page_id:
try:
all = p.page(page_id)
except PageNotAnInteger:
all = p.page(1)
except EmptyPage:
all = p.page(1)
else:
all = p.page(1)
return render(request, 'index.html', locals())
html:
add_novel:
添加小说
index:
{% load static %}
{# 登录#}
小说名称 | 小说图片 |
{{ a.name }} |
{% if all.has_previous %}
上一页
{% else %}
上一页
{% endif %}
{% for num_ in p.page_range %}
{% if all.number == num_ %}
{{ num_ }}
{% else %}
{{ num_ }}
{% endif %}
{% endfor %}
{% if all.has_next %}
下一页
{% else %}
下一页
{% endif %}
login:略
show_novel:
{% load static %}
小说名称 | 小说作者 | 小说简述 | 小说图片 | 发布时间 |
{{ n.name }} | {{ n.author.author_name }} | {{ n.resume }} | {{ n.publish_time }} |
show_own_novel:
小说名称 | 小说删除 |
{{ o.name }} | 删除 |
添加小说
upload: