欢迎回到我使用 Django 而不使用 ChatGPT 构建博客应用程序的旅程。本周,我专注于实现关键功能来增强我的博客应用程序。我添加了一项功能,允许用户创建帖子、评论帖子以及为其他用户的帖子点赞。
python manage.py startapp posts
我从设计 Post 模型开始。该模型将存储用户创建的博客文章:
from django.db import models from django.contrib.auth.models import User from django.utils import timezone class Post(models.Model): title = models.CharField(max_length=100) description = models.CharField(max_length=200, help_text="Excerpt of the post...") content = models.TextField() author = models.ForeignKey(User, on_delete=models.CASCADE) publication_date = models.DateTimeField(default=timezone.now) def __str__(self): return self.title
接下来,我还创建了评论和点赞模型,以允许用户在帖子上发表评论和点赞:
class Comment(models.Model): post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE) author = models.ForeignKey(User, on_delete=models.CASCADE) content = models.TextField() created_at = models.DateTimeField(default=timezone.now) def __str__(self): return f"Comment by {self.author} on {self.post}" class Like(models.Model): post = models.ForeignKey(Post, related_name="likes", on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE) created_at = models.DateTimeField(default=timezone.now) def __str__(self): return f"Like by {self.user} on {self.post}"
我也不会包含模板。我创建了用于创建、列出所有帖子和显示单个帖子的视图:
from django.shortcuts import render, redirect, get_object_or_404 from rest_framework import generics from django.contrib.auth.decorators import login_required from .serializer import PostSerializer from .forms import * from .models import * def create_post(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): newPost = form.save(commit=False) newPost.author = request.user newPost.save() return redirect("posts:my-posts") else: form = PostForm() return render(request, "create_post.html", {"form": form, "show_header": True}) @login_required def myPosts(request): ''' List the user created posts ''' posts = Post.objects.filter(author=request.user) return render(request, "posts_list.html", {"posts": posts, "show_header": True}) def postDetails(request, pk): ''' List all posts in the dashboard and display the comments and likes ''' post = get_object_or_404(Post, pk=pk) comment = post.comments.all() likes = post.likes.count() user_has_liked = post.likes.filter(user=request.user).exists() if request.method == "POST": comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.author = request.user comment.save() return redirect("posts:post-detail", pk=post.pk) else: comment_form = CommentForm() return render( request, "main_post.html", { "post": post, "comments": comment, "comment_form": comment_form, "likes": likes, "user_has_liked": user_has_liked, }, ) @login_required def like_post(request, pk): ''' Handle liking posts ''' post = get_object_or_404(Post, pk=pk) like, created = Like.objects.get_or_create(post=post, user=request.user) if not created: like.delete() return redirect("posts:post-detail", pk=post.pk)
from django.urls import path from .views import * app_name = "posts" urlpatterns = [ path("createpost/", create_post, name="create-post"), path("post/<int:pk>/", postDetails, name="post-detail"), path("post/<int:pk>/like/", like_post, name="like-post"), path("myposts/", myPosts, name="my-posts"), ]
本周,我遇到了以下挑战:
尽管面临这些挑战,我还是学到了很多关于处理用户交互以及使用 Django 的 ORM 来管理模型之间的关系的知识。
这就是我的简单博客应用程序。检查我的 Github 了解更多详细信息和应用程序的现场演示。
我计划创建另一个 Django 项目来提高我的技能和知识。
如果您对 Django 项目的好项目或合作有任何想法,请随时发表评论。
请继续关注我的旅程的更多更新!
以上是创建简单博客应用程序的一周的详细内容。更多信息请关注PHP中文网其他相关文章!