django 测试6小说

该博客展示了基于Python和Django框架的小说管理系统代码。包含urls、setting、models、views等部分代码,实现了小说的添加、展示、删除等功能,还有登录、分页展示等功能,同时给出了相关的HTML页面代码。

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/<int:id>/', views.show_novel),
path('show_own_novel/', views.show_own_novel),
path('del_novel/<int:id>', views.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:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>添加小说</h3>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
小说名称:<input type="text" name="name">
小说简介:<input type="text" name="resume">
小说图片:<input type="file" name="pic">
<button type="submit">添加</button>
</form>
</body>
</html>

 

index:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<form method='get' action="/search/" target="_blank">
<input type="text" name="q">
<input type="submit" value="查询">
</form>
{% load static %}
{# <a href="/login/">登录</a>#}
<table border="1">
<tr>
<td>小说名称</td>
<td>小说图片</td>
</tr>
{% for a in all %}
<tr>
<td><a href="/show_novel/{{ a.id }}">{{ a.name }}</a></td>
<td><img src="{% static a.pic %}" style="height: 50px; width: 50px"></td>
</tr>
{% endfor %}
</table>

{% if all.has_previous %}
<a href="/?page_id={{ all.previous_page_number }}">上一页</a>
{% else %}
上一页
{% endif %}

{% for num_ in p.page_range %}
{% if all.number == num_ %}
<a href="/?page_id={{ num_ }}">{{ num_ }}</a>
{% else %}
<a href="/?page_id={{ num_ }}">{{ num_ }}</a>
{% endif %}
{% endfor %}

{% if all.has_next %}
<a href="/?page_id={{ all.next_page_number }}">下一页</a>
{% else %}
下一页
{% endif %}

</body>
</html>

 

login:略

 

show_novel:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% load static %}
<table border="1">
<tr>
<td>小说名称</td>
<td>小说作者</td>
<td>小说简述</td>
<td>小说图片</td>
<td>发布时间</td>
</tr>
{% for n in all %}
<tr>
<td>{{ n.name }}</td>
<td>{{ n.author.author_name }}</td>
<td>{{ n.resume }}</td>
<td><img src="{% static n.pic %}" style="height: 50px; width: 50px"></td>
<td>{{ n.publish_time }}</td>
</tr>
{% endfor %}
</table>

</body>
</html>

 

 

show_own_novel:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>小说名称</td>
<td>小说删除</td>
</tr>
{% for o in own_novel %}
<tr>
<td>{{ o.name }}</td>
<td><a href="/del_novel/{{ o.id }}">删除</a></td>
</tr>
{% endfor %}
</table>
<a href="/add_novel/">添加小说</a>
</body>
</html>

 

 

upload:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
<input type="file" name="img">
<button type="submit">上传</button>
</form>
</body>
</html>

 

转载于:https://www.cnblogs.com/lhrd/p/10911147.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值