Python个人博客系统django源代码,个人网站系统源程序,基于django+mysql
使用方法:
1、pip install -r requirements.txt ,安装程序相关依赖
2、在MySQL中创建youngBlog数据库,并导入youngblog.sql,修改youngBlog/settins.py中的DATABASES设置
3、manage.py createsuperuser,创建超级管理员,按照提示输入用户名、电子邮箱地址和密码即可
4、manage.py runserver启动程序即可,打开http://127.0.0.1:8000/,后台http://127.0.0.1:8000/ login
完整程序代码下载地址:Python个人博客系统django源代码,个人网站系统源程序
"""
Django settings for youngBlog project.
Generated by 'django-admin startproject' using Django 3.1.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '7x5+6(&)#fffqog%$6zz*njxxo#e&b%pexr^^ij5@cjm7ja4%&'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myBlog.apps.MyblogConfig', #或者 myBlog
'captcha',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'youngBlog.urls'
AUTH_USER_MODEL = "myBlog.User_admin"
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',
],
},
},
]
WSGI_APPLICATION = 'youngBlog.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'youngBlog',
'USER': 'root',
'PASSWORD': 'sxing86',
'HOST': 'localhost',
'PORT': '3306',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False #
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
# 用于部署打包静态文件
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
# 静态文件地址拼接,后面'static'文件为自己建立的存放静态文件(JS,IMG,CSS)的文件名
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'), # 主文件下静态文件
)
# 添加以下代码以后就不用写{% load staticfiles %},可以直接引用
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)
#媒体文件
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,"media")
#验证码的配置
CAPTCHA_OUTPUT_FORMAT = '%(image)s %(text_field)s %(hidden_field)s '
CAPTCHA_NOISE_FUNCTIONS = ('captcha.helpers.noise_null', # 没有样式
# 'captcha.helpers.noise_arcs', # 线
# 'captcha.helpers.noise_dots', # 点
)
# 图片中的文字为随机英文字母,如 mdsh
# CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.random_char_challenge'
# 图片中的文字为数字表达式,如2+2=
CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.math_challenge'
# 超时(minutes)
CAPTCHA_TIMEOUT = 1
view.py
import datetime
import json
import uuid
from captcha.helpers import captcha_image_url
from captcha.models import CaptchaStore
from django.core import serializers
from django.core.paginator import Paginator
from django.http import JsonResponse
from django.shortcuts import render,redirect
from django.views import View
from django.contrib.auth.hashers import check_password,make_password
from myBlog.form import *
from myBlog.models import *
from django.contrib.auth import login
from django.contrib.auth.mixins import LoginRequiredMixin
class Login(View):
def get(self,request):
hashkey = CaptchaStore.generate_key()
image_url = captcha_image_url(hashkey)
login_form = UserForm()
return render(request,'login.html',locals())
def post(self,request):
yzm = request.POST.get('captcha', '')
hashkey = request.POST.get('code')
# 根据key获取验证码对象
cap = CaptchaStore.objects.filter(hashkey=hashkey).first()
if cap: # 存在
if cap.response == yzm.lower():
login_form = UserForm(request.POST)
if login_form.is_valid():
username = login_form.cleaned_data.get('username')
password = login_form.cleaned_data.get('password')
try:
user = User_admin.objects.get(username=username)
except:
message = '用户不存在!'
return redirect('/login/')
#使用django自带的密码验证
if check_password(password, user.password):
login(request, user)
return redirect('/admin/')
else:
message = '密码不正确!'
return redirect('/login/')
else:
return redirect('/login/')
else:
message = '验证码无效!'
return redirect('/login/')
#登出
from django.contrib.auth import logout
class Loginout(View):
def get(self,request):
logout(request)
return redirect('/login/')
#后台
class Admin(LoginRequiredMixin,View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
def get(self,request):
username = request.user
users = User_admin.objects.filter(username=username).values('nickName','username').first()
return render(request,'index.html',{"users":users})
#首页
class Welcome(LoginRequiredMixin,View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
def get(self,request):
return render(request,'welcome.html')
#文章添加
class ArticleOpt(LoginRequiredMixin,View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
def get(self, request):
id = request.GET.get('id','')
if id == '':
return render(request, 'article-add.html')
else:
ArticleData = Article.objects.filter(id=id).first()
return render(request, 'article-add.html',{'ArticleData':ArticleData})
def post(self, request):
id = request.POST.get('id','')
title = request.POST.get('title')
tag = request.POST.get('tag')
classify_id = request.POST.get('classify_id')
content = request.POST.get('content')
status = request.POST.get('status')
isdelete = request.POST.get('isdelete',False)
if id =="":
instance = Article.objects.create(id=uuid.uuid4(),title=title,tag=tag,classify_id=classify_id,content=content,status=int(status))
instance.save()
Result = {
"code": 2000,
"msg": "添加成功",
"count": 0,
"data": ""
}
else:
if isdelete:
Article.objects.filter(id=id).delete()
Result = {
"code": 2000,
"msg": "删除成功",
"count": 0,
"data": ""
}
else:
Article.objects.filter(id=id).update(title=title, tag=tag, classify_id=classify_id, content=content,
status=int(status))
Result = {
"code": 2000,
"msg": "修改成功",
"count": 0,
"data": ""
}
return JsonResponse(Result)
#文章列表
class ArticleList(LoginRequiredMixin,View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
def get(self,request):
return render(request,'article-list.html')
def post(self,request):
reqData = request.POST
pageIndex = reqData.get('pageIndex', 1)
pageSize = reqData.get('pageSize', 10)
lists = Article.objects.all()
title = reqData.get('title',None)
status = reqData.get('status',None)
updateTime = reqData.get('updateTime',None)
if title !="":
lists = Article.objects.filter(title__icontains=title)
if status !="":
lists = Article.objects.filter(status=int(status))
if updateTime !="":
updateTime = updateTime.split(',')
start = datetime.datetime.strptime(updateTime[0][:19],'%Y-%m-%d %H:%M:%S')
end = datetime.datetime.strptime(updateTime[1][1:20],'%Y-%m-%d %H:%M:%S') + datetime.timedelta(days=1).strftime("%Y-%m-%d %H:%M:%S")
lists = Article.objects.filter(updateTime__gte=start,updateTime__lte=end)
pageInator = Paginator(lists, pageSize)
contacts = pageInator.page(pageIndex)
json_data = serializers.serialize("json", contacts,fields=('title','tag','status','createTime','updateTime',))
Result = {
"code": 2000,
"msg": "成功",
"count": len(lists),
"data": json.loads(json_data)
}
return JsonResponse(Result)
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
#图片上传
class Upload_img(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get(self,request):
data ={
"success": 0, # 0 表示上传失败,1 表示上传成功
"message": "上传失败。",
"url": "" # 上传成功时才返回
}
return JsonResponse(data)
def post(self,request):
imgs = request.FILES.get('editormd-image-file')
name = str(imgs).split('.')[0]
data = {
"name": name,
"editormdImageFile": imgs
}
form = UploadFileForm(request.POST, data)
if form.is_valid():
instance = Imgs(name=name,url=request.FILES['editormd-image-file']) #保存文件到FileField域
instance.save()
if instance.url != None:
data = {
"success": 1, # 0 表示上传失败,1 表示上传成功
"message": "上传成功",
"url": "/media/" + str(instance.url) # 上传成功时才返回
}
else:
data = {
"success": 0, # 0 表示上传失败,1 表示上传成功
"message": "上传失败",
"url": "" # 上传成功时才返回
}
else:
data = {
"success": 0, # 0 表示上传失败,1 表示上传成功
"message": "上传失败。",
"url": "" # 上传成功时才返回
}
return JsonResponse(data)
#分类添加
class ClassifyAdd(LoginRequiredMixin,View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
def get(self,request):
id = request.GET.get('id', '')
if id == '':
return render(request, 'classify-add.html')
else:
ClassifyData = Classify.objects.filter(id=id).first()
return render(request, 'classify-add.html', {'Data': ClassifyData})
def post(self,request):
id = request.POST.get('id', '')
name = request.POST.get('name')
isdelete = request.POST.get('isdelete', False)
if id == "":
instance = Classify.objects.create(id=uuid.uuid4(), name=name)
instance.save()
Result = {
"code": 2000,
"msg": "添加成功",
"count": 0,
"data": ""
}
else:
if isdelete:
Classify.objects.filter(id=id).delete()
Result = {
"code": 2000,
"msg": "删除成功",
"count": 0,
"data": ""
}
else:
Classify.objects.filter(id=id).update(name=name)
Result = {
"code": 2000,
"msg": "修改成功",
"count": 0,
"data": ""
}
return JsonResponse(Result)
#分类获取
class ClassifyList(View):
def get(self,request):
return render(request,'classify.html')
def post(self,request):
reqData = request.POST
pageIndex = reqData.get('pageIndex',1)
pageSize = reqData.get('pageSize',10)
name = reqData.get('name','')
if name !="":
lists = Classify.objects.filter(name__icontains=name)
else:
lists = Classify.objects.all()
pageInator = Paginator(lists, pageSize)
contacts = pageInator.page(pageIndex)
json_data = serializers.serialize("json", contacts)
Result = {
"code": 2000,
"msg": "成功",
"count": len(lists),
"data": json.loads(json_data)
}
return JsonResponse(Result)
#前端文章获取
class MyArticle(View):
def get(self,request):
id = request.GET.get('id',None)
classify_id = request.GET.get('classify_id',None)
if id is None:
if classify_id is None:
ArticleList = Article.objects.filter(status=1).all()
else:
ArticleList = Article.objects.filter(status=1,classify_id = classify_id).all()
pageIndex = request.GET.get('pageIndex', 1)
pageSize = request.GET.get('pageSize', 5)
pageInator = Paginator(ArticleList, pageSize)
contacts = pageInator.page(pageIndex)
return render(request,'frontEnd/index.html',{'ArticleList':contacts,'CountList':ArticleList.count()})
else:
ArticleList = Article.objects.filter(id=id).first()
return render(request, 'frontEnd/travel.html', {'ArticleList': ArticleList})
#修改密码
class UpdatePwd(LoginRequiredMixin,View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
def get(self,request):
username = request.user
return render(request,'change-password.html',{"username":username})
def post(self,request):
reqData = request.POST
oldpwd = reqData.get('oldpassword')
newpwd = reqData.get('newpassword')
user = User_admin.objects.filter(username=request.user).first()
Result = dict()
if check_password(oldpwd, user.password):
User_admin.objects.filter(username=request.user).update(password=make_password(newpwd))
Result = {
"code": 2000,
"msg": "成功",
"count": 0,
"data": ""
}
else:
Result = {
"code": 2001,
"msg": "旧密码不正确",
"count": 0,
"data": ""
}
return JsonResponse(Result)
完整程序代码下载地址:Python个人博客系统django源代码,个人网站系统源程序