From afdfb0728d17c89b0f708f24d36b528f7eab0ce9 Mon Sep 17 00:00:00 2001 From: blog <2429194235@qq.com> Date: Fri, 31 May 2024 11:27:23 +0800 Subject: [PATCH] 4.31 --- .../inspectionProfiles/Project_Default.xml | 12 ++++ .../blog/__pycache__/settings.cpython-312.pyc | Bin 3531 -> 3238 bytes 1_blog/blog/settings.py | 17 +---- .../post/__pycache__/models.cpython-312.pyc | Bin 3115 -> 4186 bytes 1_blog/post/__pycache__/urls.cpython-312.pyc | Bin 828 -> 925 bytes 1_blog/post/__pycache__/views.cpython-312.pyc | Bin 2297 -> 3431 bytes 1_blog/post/migrations/0003_comment.py | 29 +++++++++ ...ntent_alter_comment_created_at_and_more.py | 34 ++++++++++ .../__pycache__/0003_comment.cpython-312.pyc | Bin 0 -> 1558 bytes ...omment_created_at_and_more.cpython-312.pyc | Bin 0 -> 1555 bytes 1_blog/post/models.py | 15 +++++ 1_blog/post/search_indexes.py | 17 ----- 1_blog/post/templates/detail.html | 11 ++++ 1_blog/post/urls.py | 15 ++--- 1_blog/post/views.py | 61 ++++++++++++------ 1_blog/templates/search/__init__.py | 0 .../search/indexes/post/post_text.txt | 2 - 17 files changed, 152 insertions(+), 61 deletions(-) create mode 100644 1_blog/.idea/inspectionProfiles/Project_Default.xml create mode 100644 1_blog/post/migrations/0003_comment.py create mode 100644 1_blog/post/migrations/0004_alter_comment_content_alter_comment_created_at_and_more.py create mode 100644 1_blog/post/migrations/__pycache__/0003_comment.cpython-312.pyc create mode 100644 1_blog/post/migrations/__pycache__/0004_alter_comment_content_alter_comment_created_at_and_more.cpython-312.pyc delete mode 100644 1_blog/post/search_indexes.py delete mode 100644 1_blog/templates/search/__init__.py delete mode 100644 1_blog/templates/search/indexes/post/post_text.txt diff --git a/1_blog/.idea/inspectionProfiles/Project_Default.xml b/1_blog/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..f4c49ad --- /dev/null +++ b/1_blog/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/1_blog/blog/__pycache__/settings.cpython-312.pyc b/1_blog/blog/__pycache__/settings.cpython-312.pyc index 3c9dd4043311d78f7cc0e90ef31c242b78fa115d..653a8afbba7c4a652322e2efb36c5c7c14a9d568 100644 GIT binary patch delta 115 zcmX>ty-bqtG%qg~0}$L&jZ7=!pU5Y{^p0_(#yduqN+wO!%?V5mjEtOdvEM`XG&0!poj7*wxn^n0I7$ec_P`lg_s)nK1prv Q=HX!ExX2(T{Sf z8KdM~l>=A{(tm1pGV7iYvn`Fi1Cu4`U;W?t$o9;jqyUP@|(CR33x(E3{} z0gfRaRT^-UiZj#m5_5|6f>IN6N-}d(gTefOqWt94;^O=wO@&+R@$o77$?@@NaHJp1U*1vLO^08E2oUi6en2TemzIChb9!8c|V_dGZzgSE7lwH zstDrJdKXhmbbD}~oZ&cZ``;17fDBxO1YD#xx3m*o@1jCF01BxOXyy+8#Dwfq0~|`VyBgYE+1!c0%shXp(Vi@$z&1n)26!G_8V6;sTa_iz=bn%u z76tSbJ$am2`5+LGyNO%_dbUh>G4pYAC-!bHwjq$P6%knhR_!cq?98v_NR4&}RKMu+ zKY(1c)mS!Djza$3qC~nV$uL5=j8l|g{LLNp-O?|sZrF*!^**u_OtZ}%DC=#yRi|Kr zR$RLfgAGFwhGGO7?T3;KM|~sEj(sHSb)BEUz%iV#TKs={>I(U71k(nY7| zZp_lr_Pi3C0g?FZ*C2?2M_>%#Kbip&Q4SWtZ6J`&J-j`%pUdci)d&>)tJSy|fLJ%3 zpPNV&6%~qNKMulO1wp#d9&3s;EhsO@L}6{hT9=^f^J*<9#Mo;7fyW* zLo4<(&c>9cS+n9nnv06*Gw3 hc#DcNnqjY&AAlmF?x4v+G=ztrd1t+c06q0J{Q*u;Kd3Ai}6pBn}h+0Pa>4bN~PV diff --git a/1_blog/post/__pycache__/urls.cpython-312.pyc b/1_blog/post/__pycache__/urls.cpython-312.pyc index 00481a2121471706a5b3d10ebf8c77e7c79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Pk6qQRdk!u=o6sdq@*p$S~X+gGrPFKl-x_!8dfAb@_nKLm5$KmY&$ diff --git a/1_blog/post/migrations/0003_comment.py b/1_blog/post/migrations/0003_comment.py new file mode 100644 index 0000000..ee0c65b --- /dev/null +++ b/1_blog/post/migrations/0003_comment.py @@ -0,0 +1,29 @@ +# Generated by Django 5.0.6 on 2024-05-31 08:55 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('post', '0002_alter_category_id_alter_post_content_alter_post_id_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='Comment', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.TextField()), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='post.post')), + ], + options={ + 'verbose_name_plural': '评论', + 'db_table': 'comments', + 'ordering': ['-created_at'], + }, + ), + ] diff --git a/1_blog/post/migrations/0004_alter_comment_content_alter_comment_created_at_and_more.py b/1_blog/post/migrations/0004_alter_comment_content_alter_comment_created_at_and_more.py new file mode 100644 index 0000000..49e8c23 --- /dev/null +++ b/1_blog/post/migrations/0004_alter_comment_content_alter_comment_created_at_and_more.py @@ -0,0 +1,34 @@ +# Generated by Django 5.0.6 on 2024-05-31 09:09 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('post', '0003_comment'), + ] + + operations = [ + migrations.AlterField( + model_name='comment', + name='content', + field=models.TextField(verbose_name='内容'), + ), + migrations.AlterField( + model_name='comment', + name='created_at', + field=models.DateTimeField(auto_now_add=True, verbose_name='创建时间'), + ), + migrations.AlterField( + model_name='comment', + name='post', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='post.post', verbose_name='帖子'), + ), + migrations.AlterField( + model_name='comment', + name='updated_at', + field=models.DateTimeField(auto_now=True, verbose_name='更新时间'), + ), + ] diff --git a/1_blog/post/migrations/__pycache__/0003_comment.cpython-312.pyc b/1_blog/post/migrations/__pycache__/0003_comment.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e6f9a5e0de68773f3e83fa151ac71aca52912545 GIT binary patch literal 1558 zcmaJ>OKclO7@mD(ZLghlohJ!y4N`<+D|JI7Ul2m%<%&mBBtoQEmR4)e#NDtDv%3y; zE)*#T9zyB`nghp52qofiYRZWt5|_0a$<|yzB0=G1l=Q*@@z1UeMnud?|Nf6}{(t6w z%#VYEGJ^GIdSmlx5usn0&|3pt=BxnBeS{I_3{>Rc&l|ic6a`Z(iX7t64TObVghe83 z1>SlN!vDSy{)+<*E;H~O6IL@kl})|sXr69cE|p9h6T_{9z>p4hg|khLMJGYzB9FN$ zR}^?e#EW8K9y6P?HPr7aQ^H+6w7^HtoHZn(J1fs2W^ORjI5(zHr?jz$aAoow{bQT};BH zMl4LMicVaI4HS(p1*yBqCdXk7O%scHbpeuEU@>THjok4LZrCC**^0*8KXBmKjgn})+xwNpnLS?%~x*QC z=1c#)0VU|Nd1$1dQF670!?GR_RyVf|WEXWL6nl&72>hsY_w|m9V(Aa$R`g0UdZnG5 z`Z+n~Ph{_Hw7fK6rTO@dUtU`SZHafQpiuO?q!vV_E3Z(MKMX$AAZ^h32W8W#xrqj?T^`A zw{Z`MO0=RNq)3RQ5?7=a#Xt_I4fMjj7c51xH5U$zqjIy9aN)$vtT%O=f|$db_ujny zzW3(6*+2XGR6y28Bj@Ihi2(dXjbx>|%GMN7mH`Bib3mRWJMZwWkQZDrFLHnf(*O#W z02Gl>W@rgOysrrHo=?>{J@u3tCv_gHZmAg9QOWm0EV({JPFRQoW0~n1TR(G@9WmxS z54j?j7kGfg|Kox@q-RpOzZWXjp;%9?5G=atQj*mxkkSlkApW9PLK!M`b%l?8uZ6XT zVQIZ6v5wU8o4dW`r23wt@>@MdS1ljf?SmQE2m9-LS4cnUdae8hUQz45y0*L4qL}Zm z?bi=r<%^_`q^V-X4)8+ewJ7ZTP-}3x# zEgQmM4+#j;L}0ngAAYCM>dl+?HQQ1_|b23rIcJiAUr?wHxWw!FFYU}Np#-&cNb-@0Mu{6dvBEi~J=e%V<2 zX=ClGS*bZArH0=9%=16)Y66* zvrTQdp$)If>)OZLX;D#k#|^f$ERD-HwCrl`(S>#G>`NjKwzLr%InvNZex18lx?O4- z;|*iHX-qea>85e2VVrvW_55RZp##7t{Aqq0fc + +

Comments

+ {% for comment in post.comments.all %} +

{{ comment.author.username }} says: {{ comment.content }}

+ {% endfor %} +

Add a comment

+
+ {% csrf_token %} + {{ form.as_p }} + +
{% endblock %} \ No newline at end of file diff --git a/1_blog/post/urls.py b/1_blog/post/urls.py index ad55a0f..e5d6e93 100644 --- a/1_blog/post/urls.py +++ b/1_blog/post/urls.py @@ -1,12 +1,11 @@ -#coding=utf-8 - from django.urls import path, re_path from . import views # 确保从当前目录导入views模块,前面的import语句有误 urlpatterns = [ - path('', views.queryAll, name='home'), - re_path(r'^page/(?P\d+)$', views.queryAll, name='pagination'), - re_path(r'^post/(?P\d+)$', views.detail, name='post_detail'), - re_path(r'^category/(?P\d+)$', views.queryPostByCid, name='category_posts'), - re_path(r'^archive/(?P\d+)/(?P\d+)$', views.queryPostByCreated, name='archive'), -] \ No newline at end of file + path('', views.queryAll, name='home'), # 首页 + re_path(r'^page/(?P\d+)$', views.queryAll, name='pagination'), # 分页 + re_path(r'^post/(?P\d+)$', views.detail, name='post_detail'), # 帖子详情 + re_path(r'^category/(?P\d+)$', views.queryPostByCid, name='category_posts'), # 分类 + re_path(r'^archive/(?P\d+)/(?P\d+)$', views.queryPostByCreated, name='archive'), # 归档 + path('/comments/', views.post_detail, name='post_comments'), # 帖单评论页 +] diff --git a/1_blog/post/views.py b/1_blog/post/views.py index 8c451c7..ebf3267 100644 --- a/1_blog/post/views.py +++ b/1_blog/post/views.py @@ -1,26 +1,27 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals +import math + +from django.core.paginator import Paginator +from django.shortcuts import get_object_or_404, redirect, render -from django.shortcuts import render # Create your views here. -#渲染主页面 +# 渲染主页面 from post.models import Post -from django.core.paginator import Paginator -import math -def queryAll(request,num=1): +from django import forms + + +def queryAll(request, num=1): num = int(num) #获取所有帖子信息 postList = Post.objects.all().order_by('-created') #创建分页器对象 - pageObj = Paginator(postList,1) + pageObj = Paginator(postList, 1) #获取当前页的数据 perPageList = pageObj.page(num) - #生成页码数列表 # 每页开始页码 begin = (num - int(math.ceil(10.0 / 2))) @@ -39,27 +40,51 @@ def queryAll(request,num=1): pageList = range(begin, end + 1) + return render(request, 'index.html', {'postList': perPageList, 'pageList': pageList, 'currentNum': num}) - return render(request,'index.html',{'postList':perPageList,'pageList':pageList,'currentNum':num}) #阅读全文功能 -def detail(request,postid): +def detail(request, postid): postid = int(postid) #根据postid查询帖子的详情信息 post = Post.objects.get(id=postid) - return render(request,'detail.html',{'post':post}) -#根据类别id查询所有帖子 -def queryPostByCid(request,cid): + return render(request, 'detail.html', {'post': post}) + +#根据类别id查询所有帖子 +def queryPostByCid(request, cid): postList = Post.objects.filter(category_id=cid) # Post.objects.filter(category__id=cid) - return render(request,'article.html',{'postList':postList}) + return render(request, 'article.html', {'postList': postList}) + #根据发帖时间查询所有帖子 -def queryPostByCreated(request,year,month): +def queryPostByCreated(request, year, month): + postList = Post.objects.filter(created__year=year, created__month=month) + return render(request, 'article.html', {'postList': postList}) + + +def post_detail(request, pk): + post = get_object_or_404(Post, pk=pk) + if request.method == 'POST': + form = CommentForm(request.POST) + if form.is_valid(): + comment = form.save(commit=False) + comment.post = post + comment.save() + return redirect('detail', pk=post.pk) # 重定向到文章详情页 + else: + form = CommentForm() + + context = { + 'post': post, + 'form': form, + } + return render(request, 'detail.html', context) + - postList = Post.objects.filter(created__year=year,created__month=month) - return render(request,'article.html',{'postList':postList}) \ No newline at end of file +class CommentForm(forms.Form): + content = forms.CharField(widget=forms.Textarea(attrs={'rows': '4', 'cols': '60'})) diff --git a/1_blog/templates/search/__init__.py b/1_blog/templates/search/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/1_blog/templates/search/indexes/post/post_text.txt b/1_blog/templates/search/indexes/post/post_text.txt deleted file mode 100644 index 9223bc5..0000000 --- a/1_blog/templates/search/indexes/post/post_text.txt +++ /dev/null @@ -1,2 +0,0 @@ -{{object.title}} -{{object.content}} \ No newline at end of file