歡迎回到我使用 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//", postDetails, name="post-detail"), path("post/ /like/", like_post, name="like-post"), path("myposts/", myPosts, name="my-posts"), ]
本週,我遇到了以下挑戰:
儘管面臨這些挑戰,我還是學到了很多關於處理使用者互動以及使用 Django 的 ORM 來管理模型之間的關係的知識。
這就是我的簡單部落格應用程式。檢查我的 Github 以了解更多詳細資訊和應用程式的現場演示。
我計劃創建另一個 Django 專案來提高我的技能和知識。
如果您對 Django 專案的好專案或合作有任何想法,請隨時發表評論。
請繼續關注我的旅程的更多更新!
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3