first commit

main
谢英豪 6 months ago
parent 38e5ce5ed7
commit edfcc247ed

@ -16,7 +16,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.venv" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.12 (DjangoBlogs) (2)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="TemplatesService"> <component name="TemplatesService">

@ -3,5 +3,5 @@
<component name="Black"> <component name="Black">
<option name="sdkName" value="Python 3.12 (DjangoBlogs)" /> <option name="sdkName" value="Python 3.12 (DjangoBlogs)" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (DjangoBlogs)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (DjangoBlogs) (2)" project-jdk-type="Python SDK" />
</project> </project>

@ -143,3 +143,4 @@ EMAIL_HOST_USER = '3485393348@qq.com'
EMAIL_HOST_PASSWORD = 'grjwnwpflcohdagg' EMAIL_HOST_PASSWORD = 'grjwnwpflcohdagg'
DEFAULT_FROM_EMAIL = '3485393348@qq.com' DEFAULT_FROM_EMAIL = '3485393348@qq.com'
LOGIN_URL = '/auth/login'

@ -0,0 +1,38 @@
from django import forms
from django.contrib.auth import get_user_model
from .models import CaptchaModel
User = get_user_model()
class RegisterForm(forms.Form):
username = forms.CharField(max_length=20, min_length=2, error_messages={
'required': '请传入用户名!',
"max_length":'用户名长度在2~20之间',
"min_length": '用户名长度在2~20之间'
})
email = forms.EmailField(error_messages={"required": '请传入邮箱!', 'invalid': '请传入一个正确的邮箱!'})
captcha = forms.CharField(max_length=4, min_length=4)
password = forms.CharField(max_length=20, min_length=6)
def clean_email(self):
email = self.cleaned_data.get('email')
exists = User.objects.filter(email=email).exists()
if exists:
raise forms.ValidationError('邮箱已经被注册!')
return email
def clean_captcha(self):
captcha = self.cleaned_data.get('captcha')
email = self.cleaned_data.get('email')
captcha_model = CaptchaModel.objects.filter(email=email, captcha=captcha).first()
if not captcha_model:
raise forms.ValidationError("验证码和邮箱不匹配!")
captcha_model.delete()
return captcha
class LoginForm(forms.Form):
email = forms.EmailField(error_messages={"required": '请传入邮箱!', 'invalid': '请传入一个正确的邮箱!'})
password = forms.CharField(max_length=20, min_length=6)
remember = forms.IntegerField(required=False)

@ -4,7 +4,8 @@ from .import views
app_name = 'auth1' app_name = 'auth1'
urlpatterns=[ urlpatterns=[
path('login',views.login, name='login'), path('login',views.login1, name='login'),
path('logout',views.logout1, name='logout'),
path('register',views.register, name='register'), path('register',views.register, name='register'),
path('captcha', views.send_email_captcha, name='email_captcha') path('captcha', views.send_email_captcha, name='email_captcha')
] ]

@ -1,16 +1,63 @@
from django.shortcuts import render from django.shortcuts import render, redirect, reverse
from django.http.response import JsonResponse from django.http.response import JsonResponse
import string import string
import random import random
from django.core.mail import send_mail from django.core.mail import send_mail
from .models import CaptchaModel from .models import CaptchaModel
from django.views.decorators.http import require_http_methods
from .forms import RegisterForm, LoginForm
from django.contrib.auth import get_user_model, login, logout
from django.contrib.auth.models import User
User = get_user_model()
# Create your views here. # Create your views here.
def login(request): @require_http_methods(['GET', 'POST'])
return render(request, 'login.html') def login1(request):
if request.method == 'GET':
return render(request, 'login.html')
else:
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data.get('email')
password = form.cleaned_data.get('password')
remember = form.cleaned_data.get('remember')
user = User.objects.filter(email=email).first()
if user and user.check_password(password):
# 登录
login(request, user)
# 判断是否需要记住我
if not remember:
# 如果没有点击记住我那么就要设置过期时间为0即浏览器关闭后就会过期
request.session.set_expiry(0)
# 如果点击了那么就什么都不做使用默认的2周的过期时间
return redirect('/')
else:
print('邮箱或密码错误!')
return redirect(reverse('auth1:login'))
def logout1(request):
logout(request)
return redirect('/')
@require_http_methods(['GET', 'POST'])
def register(request): def register(request):
return render(request, 'register.html') if request.method == 'GET':
return render(request, 'register.html')
else:
form = RegisterForm(request.POST)
if form.is_valid():
email = form.cleaned_data.get('email')
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
User.objects.create_user(email=email, username=username, password=password)
return redirect(reverse('auth1:login'))
else:
print(form.errors)
return redirect(reverse('auth1:register'))
def send_email_captcha(request): def send_email_captcha(request):

@ -0,0 +1,45 @@
# Generated by Django 5.0.6 on 2024-05-29 08:39
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='BlogCategory',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200, verbose_name='分类名称')),
],
),
migrations.CreateModel(
name='Blog',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200, verbose_name='标题')),
('content', models.TextField(verbose_name='内容')),
('pub_time', models.DateTimeField(auto_now_add=True, verbose_name='发布时间')),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='作者')),
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.blogcategory', verbose_name='分类')),
],
),
migrations.CreateModel(
name='BlogComment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.TextField(verbose_name='内容')),
('pub_time', models.DateTimeField(auto_now_add=True, verbose_name='发布时间')),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='作者')),
('blog', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.blog', verbose_name='所属博客')),
],
),
]

@ -1,3 +1,26 @@
from django.db import models from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
class BlogCategory(models.Model):
name = models.CharField(max_length=200, verbose_name='分类名称')
class Blog(models.Model):
title = models.CharField(max_length=200, verbose_name='标题')
content = models.TextField(verbose_name='内容')
pub_time = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
category = models.ForeignKey(BlogCategory, on_delete=models.CASCADE, verbose_name='分类')
author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')
class BlogComment(models.Model):
content = models.TextField(verbose_name='内容')
pub_time = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
blog = models.ForeignKey(Blog, on_delete=models.CASCADE, related_name='comments', verbose_name='所属博客')
author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')
# Create your models here.

@ -1,5 +1,6 @@
from django.shortcuts import render from django.shortcuts import render
from django.urls.base import reverse_lazy
from django.contrib.auth.decorators import login_required
# Create your views here. # Create your views here.
def index(request): def index(request):
return render(request, 'index.html') return render(request, 'index.html')
@ -7,5 +8,6 @@ def index(request):
def blog_detail(request, blog_id): def blog_detail(request, blog_id):
return render(request,'blog_detail.html') return render(request,'blog_detail.html')
@login_required()
def pub_blog(request): def pub_blog(request):
return render(request,'pub_blog.html') return render(request,'pub_blog.html')

@ -1,5 +1,5 @@
[client] [client]
database = djangoblog database = djangoblog
user = root user = root
password = huixia1222 password = root
default-character-set = utf8 default-character-set = utf8

@ -18,8 +18,8 @@
</a> </a>
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0"> <ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
<li><a href="#" class="nav-link px-2 text-white">论坛首页</a></li> <li><a href="/" class="nav-link px-2 text-white">论坛首页</a></li>
<li><a href="#" class="nav-link px-2 text-white">发布</a></li> <li><a href="{% url 'blog:pub_blog' %}" class="nav-link px-2 text-white">发布</a></li>
<li><a href="#" class="nav-link px-2 text-white">Pricing</a></li> <li><a href="#" class="nav-link px-2 text-white">Pricing</a></li>
<li><a href="#" class="nav-link px-2 text-white">FAQs</a></li> <li><a href="#" class="nav-link px-2 text-white">FAQs</a></li>
<li><a href="#" class="nav-link px-2 text-white">About</a></li> <li><a href="#" class="nav-link px-2 text-white">About</a></li>
@ -29,11 +29,25 @@
<input type="search" class="form-control form-control-dark text-white" placeholder="搜索..." <input type="search" class="form-control form-control-dark text-white" placeholder="搜索..."
aria-label="Search" _mstplaceholder="99723" _mstaria-label="74607"> aria-label="Search" _mstplaceholder="99723" _mstaria-label="74607">
</form> </form>
{% if user.is_authenticated %}
<div class="dropdown text-end">
<a href="#" class="d-block link-body-emphasis text-decoration-none dropdown-toggle"
data-bs-toggle="dropdown" aria-expanded="false">
<img src="{% static 'image/profile.png' %}" alt="mdo" width="32" height="32" class="rounded-circle">
</a>
<ul class="dropdown-menu text-small" style="">
<li><a class="dropdown-item" href=" {% url 'auth1:logout' %} ">退出登录</a></li>
</ul>
</div>
{% else %}
<div class="text-end">
<a href="{% url 'auth1:login' %}" type="button" class="btn btn-outline-primary me-2">登录</a>
<a href="{% url 'auth1:register' %}" type="button" class="btn btn-primary">注册</a>
</div>
{% endif %}
<div class="text-end">
<button type="button" class="btn btn-outline-light me-2">登录</button>
<button type="button" class="btn btn-outline-light me-2">注册</button>
</div>
</div> </div>
</div> </div>
</header> </header>

@ -6,6 +6,7 @@
<div style="max-width: 400px;" class="m-auto"> <div style="max-width: 400px;" class="m-auto">
<h1>登录</h1> <h1>登录</h1>
<form action="" method="POST"> <form action="" method="POST">
{% csrf_token %}
<div class="mb-3"> <div class="mb-3">
<label>邮箱</label> <label>邮箱</label>
<input type="email" name="email" class="form-control" placeholder="请输入邮箱"> <input type="email" name="email" class="form-control" placeholder="请输入邮箱">

@ -12,6 +12,7 @@
<div style="max-width: 400px;" class="m-auto"> <div style="max-width: 400px;" class="m-auto">
<h1>请注册</h1> <h1>请注册</h1>
<form action="" method="POST"> <form action="" method="POST">
{% csrf_token %}
<div class="mb-3"> <div class="mb-3">
<label>用户名</label> <label>用户名</label>
<input type="text" name="username" class="form-control" placeholder="用户名"> <input type="text" name="username" class="form-control" placeholder="用户名">
@ -27,7 +28,7 @@
<div class="mb-3"> <div class="mb-3">
<label>验证码</label> <label>验证码</label>
<div class="input-group "> <div class="input-group ">
<input type="text" class="form-control" placeholder="请输入验证码" <input type="text" class="form-control" name="captcha" placeholder="请输入验证码"
aria-label="Recipient's username" aria-describedby="button-addon2"> aria-label="Recipient's username" aria-describedby="button-addon2">
<button class="btn btn-outline-secondary" type="button" id="captcha-btn">获取验证码</button></div> <button class="btn btn-outline-secondary" type="button" id="captcha-btn">获取验证码</button></div>
</div> </div>

Loading…
Cancel
Save