我正在尝试在网站上发表评论,但是当我启动服务器时,看不到表格。我创建了一个简单的博客,一个人可以在其中发布喜欢和评论,但是问题是,当我添加评论表单时,它们根本不会出现。
PS
请问我是我的英语吗?我来自另一个国家,我不太会英语
post.html主模板
{% extends "ShapeHtml/wrapper.html" %}
{% block content %}
<div class="panel panel-default">
<div class="panel-heading">
<h1 class=" text-info">{{object.title}}</h1>
</div>
<div class="panel-body">
<p> {{object.post|safe|linebreaks}} </p>
<h3 align="right" class=" text-info"> Опубликованно: {{articles.date|date:"d-m-Y в H:i"}}</h3>
</div>
<h4>Comments</h4>
<form action="{% url '' %}" method="post">
{% csrf_token %}
{% if CommentModel %}
{% for CommentModel in comments %}
{{ CommentModel.WhoAreYou }} <br>
{% endfor %}
{% endif %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %}
views.py
from .forms import CommentForm
class ArticlesList(ListView):
model = Articles
template_name = 'news/posts.html'
class ArticleDetail(DetailView):
model = Articles
template_name = 'news/post.html'
def GetComments(request):
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(request.path_info)
else:
form = CommentForm()
comments = CommentModel.objects.all()
return render(request, 'news/post.html', {'form': form, 'comments': comments})
urls.py
urlpatterns=[
path('', ArticlesList.as_view(), name='articles_list'),
path('<int:pk>/', ArticleDetail.as_view(), name='article_detail'),
path('aboutUs', views.aboutUs, name='aboutUs'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py是帖子和评论的模型
from django.db import models
class Articles(models.Model):
title = models.CharField(max_length= 200)
post = models.TextField()
date = models.DateTimeField()
img = models.ImageField(upload_to='', default="default_value")
def __str__(self):
return self.title
class CommentModel(models.Model):
WhoAreYou = models.CharField(max_length=100)
forms.py字段表单
CommentForm(ModelForm)类
class Meta:
model = CommentModel
fields = ('WhoAreYou',)
答案 0 :(得分:-1)
您已经在form.py中写了class CommentForm(forms.ModelForm)
:
class CommentForm(forms.ModelForm):
comment = forms.CharField()
class Meta:
model = CommentModel
fields = ('WhoAreYou',)
我想它会起作用的。