我还在学习django。试图在我的blogdetail视图中创建评论表单。.我正在关注youtube上的教程。
https://www.youtube.com/watch?v=An4hW4TjKhE&t=40s
本教程具有从django用户那里获取用户的表格..但是在我的表格中,我希望用户输入他的姓名,电子邮件和内容。 按照本教程,我制作了表单模型,但是我不知道该如何从自己的html表单中获取数据。
我已经达到了一个阶段,在该阶段中,我可以在admin中添加注释并将其显示在我的html文件中,但是现在我遇到了错误。
name 'post' is not defined
我的文件是..
forms.py
from django import forms
from.models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('content', 'email', 'name' ,)
models.py
class BlogPost(models.Model):
title = models.CharField(max_length=500)
writer = models.CharField(max_length=150,default='my dept')
category =models.CharField(max_length=150)
image = models.ImageField(upload_to='images')
post = models.TextField(max_length=2000)
Date = models.DateField( default=datetime.date.today)
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(BlogPost , on_delete=models.CASCADE)
name = models.CharField (max_length = 150)
email = models.CharField (max_length = 150)
content = models.TextField ()
def __str__(self):
return self.email
views.py
def detailview(request, id=None):
blg = get_object_or_404(BlogPost, id=id)
comments = Comment.objects.filter( post=blg).order_by('-id')
if request.method == 'POST':
comment_form = CommentForm(request.POST or None )
if comment_form.is_valid():
content = request.POST.get('content')
Comment.objects.create(post= post , content=content)
comment_form.save()
else:
comment_form = CommentForm()
context = {'blg': blg,
'comments': comments,
'comment_form' : comment_form,
}
return render(request, 'blog/blogdetail.html', context)
blogdetail.html
<div id="respond" class="clearfix">
<div class="title-box">
<h3>Leave a <strong>Comment</strong></h3>
</div><!-- end title-box -->
<div class="row">
<!-- start Comment Form -->
<form class="clearfix" action="#" method="post" id="commentform">
{% csrf_token %}
<p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p>
<div class="form-group">
<div class="col-sm-12">
<label>Comment<span class="required">*</span></label>
<textarea name="comment" cols="58" rows="7" tabindex="4" class="form-control" required >{{comment.content}}</textarea>
</div>
</div><!-- end form-group -->
<div class="form-group">
<div class="col-sm-6">
<label>Name<span class="required">*</span></label>
<input name="author" id="author" value="" size="22" tabindex="1" class="form-control" type="text" required >{{comment.name}}</div>
<div class="col-sm-6">
<label>Email<span class="required">*</span></label>
<input name="email" id="email" value="" size="22" tabindex="2" class="form-control" type="text" required >{{comment.email}}</div>
</div><!-- end form-group -->
<div class="form-group">
<div class="col-sm-12">
<button name="submit" type="submit" id="submit-button" tabindex="5" value="Submit" class="btn btn-shutter-out-horizontal btn-lg">Submit Comment</button>
</div>
</div><!-- end form-group -->
</form>
<!-- end Comment Form -->
</div>
</div><!-- #respond end -->
实际上看起来像这样
[![评论形式] [1]] [1]
我想从前端发布名称电子邮件和评论..浪费了将近2天,没有成功..请帮助.. [1]:https://i.stack.imgur.com/HjDZ6.png
答案 0 :(得分:0)
func rotateLeft(array:[Int],numberOfRotation:Int) -> [Int]
{
let offset = numberOfRotation % array.count
let tempResult = array[offset...] + array[..<offset]
return Array(tempResult)
}
答案 1 :(得分:0)
由于您是从html手动传递数据,因此请重写此功能
if comment_form.is_valid():
content = request.POST.get('content', '')
email = request.POST.get('email', '')
name = request.POST.get('author', '')
comment =Comment.objects.create(post= blg, email=email, name=name, content=content)
return redirect('posts-detail', pk=comment.id )<this can be how you like it>
您所缺少的是在html中获取丢失的字段,这些字段取自输入中的名称属性