制作一个具有文本帖子论坛和图像温室页面的网站,当我尝试在论坛页面上发帖时,它会将其发布到温室页面,当我尝试在论坛页面中的帖子上添加评论时我得到:
PlantPost matching query does not exist.
Request Method: POST
Request URL: http://127.0.0.1:8000/greenhouse/create_comment/1/
Django Version: 5.0.4
Exception Type: DoesNotExist
Exception Value:
PlantPost matching query does not exist.
Exception Location: C:\Users\hurle\OneDrive\Documents\Desktop\profile_env\Lib\site-packages\django\db\models\query.py, line 649, in get
Raised during: greenhouse.views.create_comment
Python Executable: C:\Users\hurle\OneDrive\Documents\Desktop\profile_env\Scripts\python.exe
Python Version: 3.12.1
Python Path:
['C:\\Users\\hurle\\OneDrive\\Documents\\Desktop\\bloomhub4',
'C:\\Users\\hurle\\AppData\\Local\\Programs\\Python\\Python312\\python312.zip',
'C:\\Users\\hurle\\AppData\\Local\\Programs\\Python\\Python312\\DLLs',
'C:\\Users\\hurle\\AppData\\Local\\Programs\\Python\\Python312\\Lib',
'C:\\Users\\hurle\\AppData\\Local\\Programs\\Python\\Python312',
'C:\\Users\\hurle\\OneDrive\\Documents\\Desktop\\profile_env',
'C:\\Users\\hurle\\OneDrive\\Documents\\Desktop\\profile_env\\Lib\\site-packages']
论坛create_comment.html
{% extends "home.html" %}
{% block content %}
<h2>Add Comment</h2>
<form action="{% url 'forum:create_comment' post.id %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock %}```
**forum create_post.html**
```{% extends "home.html" %}
{% block content %}
<h2>Create Post</h2>
<form action="{% url 'create_post' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock %}
论坛模型.py
from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField()
image = models.ImageField(upload_to="forum_images/", null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="forum_comments")
论坛浏览量.py
from django.shortcuts import render, redirect
from .models import Post, Comment
from .forms import PostForm, CommentForm
def forum(request):
posts = Post.objects.all()
comment_form = CommentForm()
return render(request, "forum.html", {"posts": posts, "comment_form": comment_form})
def create_post(request):
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
return redirect("forum")
else:
form = PostForm()
return render(request, "create_post.html", {"form": form})
def create_comment(request, post_id):
post = Post.objects.get(id=post_id)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.user = request.user
comment.post = post
comment.save()
return redirect("forum")
else:
form = CommentForm()
return render(request, "create_comment.html", {"form": form, "post": post})
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.forum, name="forum"),
path("create_post/", views.create_post, name="create_post"),
path("create_comment/<int:post_id>/", views.create_comment, name="create_comment"),
]
我希望能够在论坛页面上发帖,两个应用程序的 create_post.html 和 create_comment.html 具有相同的名称。
在下面的代码行中似乎找不到该网址:
<form action="{% url 'forum:create_comment' post.id %}" method="post">
在 urls.py 中,我没有找到上述 url 格式,这可能是当您尝试在论坛页面上发布任何评论时出现错误的原因。
您必须在 urls.py 中添加单独的路径才能在论坛页面上发表评论,如下所示:
path("create_comment/<int:post_id>/", views.create_comment, name="create_comment"),