我正在尝试打印博客下方的评论,但单击提交按钮后出现上述错误。我只想在网页顶部显示一条成功消息,为此我编写了以下行:messages.success(request, 'your comment has been added'),但出现错误!
models.py:
from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
# Create your models here.
class Post(models.Model):
sno = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
content = models.TextField()
author = models.CharField(max_length=50)
slug = models.SlugField(max_length=200)
timeStamp = models.DateTimeField(blank=True)
def __str__(self):
return self.title + " by " + self.author
class BlogComment(models.Model):
sno = models.AutoField(primary_key=True)
comment = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
timestamp = models.DateTimeField(default=now)
urls.py:
from django.urls import path, include
from blog import views
urlpatterns = [
path('postComment', views.postComment, name='postComment'),
path('', views.blogHome, name='home'),
path('<str:slug>', views.blogPost, name='blogpost'),
]
view.py:
from django.shortcuts import render, HttpResponse, redirect
from blog.models import Post, BlogComment
def blogHome(request):
allpost = Post.objects.all()
context = {'allposts': allpost}
return render(request, 'blog/blogHome.html', context)
def blogPost(request, slug):
post = Post.objects.filter(slug=slug).first()
comments = BlogComment.objects.filter(post=post)
context = {'post': post, 'comments': comments}
return render(request, 'blog/blogPost.html', context)
慕田峪9158850
FFIVE
凤凰求蛊
相关分类