我有一个应用程序,可以让用户创建博客并允许其他用户对其他博客发表评论。问题是。为了创建评论对象,我需要博客 ID 和文本。我可以通过帖子获取文本数据,但我无法从 POST 获取博客 ID,我能想到的唯一方法是通过表单中的值字段
如何从 POST 中获取 value 字段?
我的模型
class Blog(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
description = models.TextField()
class BlogComment(models.Model):
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User)
body = models.TextField()
blog = models.ForeignKey(Blog)
我的表格.py
class BlogCommentForm(forms.ModelForm):
text = forms.CharField(required=False)
class Meta:
model = BlogComment
fields = ()
<form method ="POST"> {% csrf_token %}
<input type = "hidden" name="d" value= "blog.id" />
{{form}}
</form>
我的看法
def Blogs(request,blog_id):
form = BlogCommentForm(request.POST)
if request.method == "POST":
if form.is_valid():
text = form.cleaned_data['text']
value = form.cleaned_data['value']
form = BlogCommentForm()
blog.objects.get(pk=blog_id)
comment = BlogComment.objects.filter(blog=blog)
return render(request,'blogcomment.html',{'comment':comment,'form':form})