diff --git a/DjangoBlogs/.idea/DjangoBlogs.iml b/DjangoBlogs/.idea/DjangoBlogs.iml index 700baa8..c47af1e 100644 --- a/DjangoBlogs/.idea/DjangoBlogs.iml +++ b/DjangoBlogs/.idea/DjangoBlogs.iml @@ -16,7 +16,7 @@ - + diff --git a/DjangoBlogs/.idea/misc.xml b/DjangoBlogs/.idea/misc.xml index f0e0ef6..f30b12e 100644 --- a/DjangoBlogs/.idea/misc.xml +++ b/DjangoBlogs/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/DjangoBlogs/DjangoBlogs/__pycache__/settings.cpython-312.pyc b/DjangoBlogs/DjangoBlogs/__pycache__/settings.cpython-312.pyc index d7d4dfa..6ea2a4e 100644 Binary files a/DjangoBlogs/DjangoBlogs/__pycache__/settings.cpython-312.pyc and b/DjangoBlogs/DjangoBlogs/__pycache__/settings.cpython-312.pyc differ diff --git a/DjangoBlogs/DjangoBlogs/settings.py b/DjangoBlogs/DjangoBlogs/settings.py index 0c46f9f..b2b8274 100644 --- a/DjangoBlogs/DjangoBlogs/settings.py +++ b/DjangoBlogs/DjangoBlogs/settings.py @@ -143,3 +143,4 @@ EMAIL_HOST_USER = '3485393348@qq.com' EMAIL_HOST_PASSWORD = 'grjwnwpflcohdagg' DEFAULT_FROM_EMAIL = '3485393348@qq.com' +LOGIN_URL = '/auth/login' \ No newline at end of file diff --git a/DjangoBlogs/auth1/__pycache__/forms.cpython-312.pyc b/DjangoBlogs/auth1/__pycache__/forms.cpython-312.pyc new file mode 100644 index 0000000..ed9b3fb Binary files /dev/null and b/DjangoBlogs/auth1/__pycache__/forms.cpython-312.pyc differ diff --git a/DjangoBlogs/auth1/__pycache__/urls.cpython-312.pyc b/DjangoBlogs/auth1/__pycache__/urls.cpython-312.pyc index 6148936..bc01864 100644 Binary files a/DjangoBlogs/auth1/__pycache__/urls.cpython-312.pyc and b/DjangoBlogs/auth1/__pycache__/urls.cpython-312.pyc differ diff --git a/DjangoBlogs/auth1/__pycache__/views.cpython-312.pyc b/DjangoBlogs/auth1/__pycache__/views.cpython-312.pyc index feaea0a..8bdc82b 100644 Binary files a/DjangoBlogs/auth1/__pycache__/views.cpython-312.pyc and b/DjangoBlogs/auth1/__pycache__/views.cpython-312.pyc differ diff --git a/DjangoBlogs/auth1/forms.py b/DjangoBlogs/auth1/forms.py new file mode 100644 index 0000000..c603672 --- /dev/null +++ b/DjangoBlogs/auth1/forms.py @@ -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) \ No newline at end of file diff --git a/DjangoBlogs/auth1/urls.py b/DjangoBlogs/auth1/urls.py index 06cc34f..677919b 100644 --- a/DjangoBlogs/auth1/urls.py +++ b/DjangoBlogs/auth1/urls.py @@ -4,7 +4,8 @@ from .import views app_name = 'auth1' 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('captcha', views.send_email_captcha, name='email_captcha') ] \ No newline at end of file diff --git a/DjangoBlogs/auth1/views.py b/DjangoBlogs/auth1/views.py index c1b0e1d..752562f 100644 --- a/DjangoBlogs/auth1/views.py +++ b/DjangoBlogs/auth1/views.py @@ -1,16 +1,63 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect, reverse from django.http.response import JsonResponse import string import random from django.core.mail import send_mail 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. -def login(request): - return render(request, 'login.html') +@require_http_methods(['GET', 'POST']) +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): - 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): diff --git a/DjangoBlogs/blog/__pycache__/models.cpython-312.pyc b/DjangoBlogs/blog/__pycache__/models.cpython-312.pyc index e92a6ce..31b03fb 100644 Binary files a/DjangoBlogs/blog/__pycache__/models.cpython-312.pyc and b/DjangoBlogs/blog/__pycache__/models.cpython-312.pyc differ diff --git a/DjangoBlogs/blog/__pycache__/views.cpython-312.pyc b/DjangoBlogs/blog/__pycache__/views.cpython-312.pyc index cdcde86..4e63bfc 100644 Binary files a/DjangoBlogs/blog/__pycache__/views.cpython-312.pyc and b/DjangoBlogs/blog/__pycache__/views.cpython-312.pyc differ diff --git a/DjangoBlogs/blog/migrations/0001_initial.py b/DjangoBlogs/blog/migrations/0001_initial.py new file mode 100644 index 0000000..a3e0d0b --- /dev/null +++ b/DjangoBlogs/blog/migrations/0001_initial.py @@ -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='所属博客')), + ], + ), + ] diff --git a/DjangoBlogs/blog/migrations/__pycache__/0001_initial.cpython-312.pyc b/DjangoBlogs/blog/migrations/__pycache__/0001_initial.cpython-312.pyc new file mode 100644 index 0000000..c248270 Binary files /dev/null and b/DjangoBlogs/blog/migrations/__pycache__/0001_initial.cpython-312.pyc differ diff --git a/DjangoBlogs/blog/models.py b/DjangoBlogs/blog/models.py index 71a8362..6bdb6bf 100644 --- a/DjangoBlogs/blog/models.py +++ b/DjangoBlogs/blog/models.py @@ -1,3 +1,26 @@ 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. diff --git a/DjangoBlogs/blog/views.py b/DjangoBlogs/blog/views.py index 36e1fca..631342e 100644 --- a/DjangoBlogs/blog/views.py +++ b/DjangoBlogs/blog/views.py @@ -1,5 +1,6 @@ from django.shortcuts import render - +from django.urls.base import reverse_lazy +from django.contrib.auth.decorators import login_required # Create your views here. def index(request): return render(request, 'index.html') @@ -7,5 +8,6 @@ def index(request): def blog_detail(request, blog_id): return render(request,'blog_detail.html') +@login_required() def pub_blog(request): return render(request,'pub_blog.html') \ No newline at end of file diff --git a/DjangoBlogs/my.cnf b/DjangoBlogs/my.cnf index 8b843bf..a9ddf13 100644 --- a/DjangoBlogs/my.cnf +++ b/DjangoBlogs/my.cnf @@ -1,5 +1,5 @@ [client] database = djangoblog user = root -password = huixia1222 +password = root default-character-set = utf8 \ No newline at end of file diff --git a/DjangoBlogs/templates/base.html b/DjangoBlogs/templates/base.html index 82f9d44..c0999bf 100644 --- a/DjangoBlogs/templates/base.html +++ b/DjangoBlogs/templates/base.html @@ -18,8 +18,8 @@