我有一個應用程序可以讓用戶創建博客並允許其他用戶對每個其他博客發表評論。問題是。爲了創建評論對象,我需要博客ID和文本。我可以通過帖子獲取文本數據,但我無法從POST獲取博客ID,但我可以想到的唯一方法是通過值字段的形式Django從POST開始抓取值域
如何從POST中獲取值字段?
我的模型
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)
我forms.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})
你需要一些大括號,那裏有'value =「{{blog.id}}」' – 2013-04-26 16:24:19