Python Django不';不要保存评论
我正在建立一个论坛,用户可以发布内容并对其进行评论。我用按钮创建了评论部分,一切都很好。但是,我无法保存我的评论,在我单击“添加评论”后,它根本不会显示它们 这是我的型号。py:Python Django不';不要保存评论,python,html,django-models,django-forms,django-comments,Python,Html,Django Models,Django Forms,Django Comments,我正在建立一个论坛,用户可以发布内容并对其进行评论。我用按钮创建了评论部分,一切都很好。但是,我无法保存我的评论,在我单击“添加评论”后,它根本不会显示它们 这是我的型号。py: class Comment(models.Model): post = models.ForeignKey(BlogPost, on_delete=models.CASCADE, related
class Comment(models.Model):
post = models.ForeignKey(BlogPost,
on_delete=models.CASCADE,
related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
class Meta:
ordering = ('created',)
def __str__(self):
return 'Comment by {} on {}'.format(self.name, self.post)
{% extends 'base.html' %}
{% load static %}
{% block content %}
<style type="text/css">
.card{
max-width: 700px;
}
.card-body{
padding: 20px;
}
</style>
<div class="row">
<div id="-particles-js"></div>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script> particlesJS.load('particles-js', 'particles.json', function(){ console.log('particles.json loaded...'); }); </script>
<!-- Blog Post -->
<div class="card m-auto">
{% if blog_post.image %}
<img class="card-img-top" src="{{blog_post.image.url}}">
{% endif %}
<div class="card-body mt-2 mb-2">
<h2 class="card-title">{{blog_post.title}}</h2>
<p class="card-text">{{blog_post.body|safe}}</p>
{% if blog_post.author == request.user %}
<a href="{% url 'blog:edit' blog_post.slug %}" class="btn btn-primary">Update</a>
{% endif %}
</div>
{% for comment in comments %}
<div class="form-group">
<p class="info">
Comment {{ forloop.counter }} by {{ comment.name }}
{{ comment.created }}
</p>
{{ comment.body|linebreaks }}
</div>
{% empty %}
<p>There are no comments yet.</p>
{% endfor %}
{% if new_comment %}
<h2>Your comment has been added.</h2>
{% else %}
<form class="create-form" method="post" enctype="multipart/form-data">
{{ comment_form.as_p }}
{% csrf_token %}
<textarea class="form-control" rows="10" type="text" name="body" id="id_body" placeholder="Your comment..." required></textarea>
<p ><input type="submit" value="Add comment" class="btn btn-primary"></p>
</form>
{% endif %}
<div class="card-footer text-muted">
Updated on {{blog_post.date_updated}} by {{blog_post.author}}
</div>
</div>
</div>
</div>
{% endblock content %}
admin.py
from .models import BlogPost, Comment
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ('name', 'email', 'post', 'created', 'active')
list_filter = ('active', 'created', 'updated')
search_fields = ('name', 'email', 'body')
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('name', 'email', 'body')
from .models import BlogPost, Comment
from .forms import EmailPostForm, CommentForm
def post_detail(request, year, month, day, post):
post = get_object_or_404(BlogPost, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
# List of active comments for this post
comments = post.comments.filter(active=True)
new_comment = None
if request.method == 'POST':
# A comment was posted
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request,
'blog/post/detail.html',
{'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})
forms.py
from .models import BlogPost, Comment
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ('name', 'email', 'post', 'created', 'active')
list_filter = ('active', 'created', 'updated')
search_fields = ('name', 'email', 'body')
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('name', 'email', 'body')
from .models import BlogPost, Comment
from .forms import EmailPostForm, CommentForm
def post_detail(request, year, month, day, post):
post = get_object_or_404(BlogPost, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
# List of active comments for this post
comments = post.comments.filter(active=True)
new_comment = None
if request.method == 'POST':
# A comment was posted
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request,
'blog/post/detail.html',
{'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})
视图.py
from .models import BlogPost, Comment
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ('name', 'email', 'post', 'created', 'active')
list_filter = ('active', 'created', 'updated')
search_fields = ('name', 'email', 'body')
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('name', 'email', 'body')
from .models import BlogPost, Comment
from .forms import EmailPostForm, CommentForm
def post_detail(request, year, month, day, post):
post = get_object_or_404(BlogPost, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
# List of active comments for this post
comments = post.comments.filter(active=True)
new_comment = None
if request.method == 'POST':
# A comment was posted
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request,
'blog/post/detail.html',
{'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})
这是detail\u blog.html:
class Comment(models.Model):
post = models.ForeignKey(BlogPost,
on_delete=models.CASCADE,
related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
class Meta:
ordering = ('created',)
def __str__(self):
return 'Comment by {} on {}'.format(self.name, self.post)
{% extends 'base.html' %}
{% load static %}
{% block content %}
<style type="text/css">
.card{
max-width: 700px;
}
.card-body{
padding: 20px;
}
</style>
<div class="row">
<div id="-particles-js"></div>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script> particlesJS.load('particles-js', 'particles.json', function(){ console.log('particles.json loaded...'); }); </script>
<!-- Blog Post -->
<div class="card m-auto">
{% if blog_post.image %}
<img class="card-img-top" src="{{blog_post.image.url}}">
{% endif %}
<div class="card-body mt-2 mb-2">
<h2 class="card-title">{{blog_post.title}}</h2>
<p class="card-text">{{blog_post.body|safe}}</p>
{% if blog_post.author == request.user %}
<a href="{% url 'blog:edit' blog_post.slug %}" class="btn btn-primary">Update</a>
{% endif %}
</div>
{% for comment in comments %}
<div class="form-group">
<p class="info">
Comment {{ forloop.counter }} by {{ comment.name }}
{{ comment.created }}
</p>
{{ comment.body|linebreaks }}
</div>
{% empty %}
<p>There are no comments yet.</p>
{% endfor %}
{% if new_comment %}
<h2>Your comment has been added.</h2>
{% else %}
<form class="create-form" method="post" enctype="multipart/form-data">
{{ comment_form.as_p }}
{% csrf_token %}
<textarea class="form-control" rows="10" type="text" name="body" id="id_body" placeholder="Your comment..." required></textarea>
<p ><input type="submit" value="Add comment" class="btn btn-primary"></p>
</form>
{% endif %}
<div class="card-footer text-muted">
Updated on {{blog_post.date_updated}} by {{blog_post.author}}
</div>
</div>
</div>
</div>
{% endblock content %}
{%extends'base.html%}
{%load static%}
{%block content%}
.卡片{
最大宽度:700px;
}
.卡体{
填充:20px;
}
加载('particles-js','particles.json',function(){console.log('particles.json-loaded…');});
{%if blog_post.image%}
{%endif%}
{{blog_post.title}
{{{blog_post.body}
{%if blog_post.author==request.user%}
{%endif%}
{注释%中的注释为%}
用{Comment.name}注释{forloop.counter}}
{{comment.created}}
{{comment.body | linebreaks}}
{%empty%}
目前还没有评论
{%endfor%}
{%if new_comment%}
您的评论已添加。
{%else%}
{{comment_form.as_p}}
{%csrf_令牌%}
{%endif%}
由{{blog_post.author}在{{blog_post.date_Updated}}上更新
{%endblock内容%}
虽然我得到的一切都在正确的设置,它不起作用。我不能对这些帖子发表评论
这就是我的文件在我的目录中的位置:
任何指导都将不胜感激。谢谢大家!