在@kreigar的一些great help之后,我能够开始使用django的ModelForm而不是form.Forms。现在唯一的问题是我没有通过两个主键来关联我的评论表格: - (。
目前我的观点如下:
#views.py
@login_required
def wine_review_page(request, wine_id):
wine = get_object_or_404(Wine, pk=wine_id)
review = None
if Review.objects.filter(user=request.user, wine=wine).exists():
review = Review.objects.get(user=request.user, wine=wine)
if request.method == 'POST':
form = WineReviewForm(request.POST, instance=review)
if form.is_valid():
form.save()
return HttpResponseRedirect('/detail/%s/' % wine_id )
else:
form = WineReviewForm(instance=review)
variables = RequestContext(request, {'form': form, 'wine': wine })
return render_to_response('wine_review_page.html', variables)`
我的模型形式如下:
#models.py
class WineReviewForm(ModelForm):
class Meta:
model = Review
widgets = {
'wine': HiddenInput(),
'user': HiddenInput(),
'like_dislike': HiddenInput(),
'fave': HiddenInput(),
'sweet_dry': HiddenInput(),
'crisp_buttery: HiddenInput(),
'fruity_earthy': HiddenInput(),
'smooth_spicy': HiddenInput(),
'light_robust': HiddenInput(),
'label_rating': HiddenInput()
}
Wine_review_form:
<form method="post" action="/review/{{ wine.id }}/">
{% csrf_token %}
{{form.as_p}}
<div id="form_div" data-role="fieldcontain">
<script>
$('#form_div > input').hide();
</script>
{% if wine.wine_kind == 'whites' %}
<div class="slider_labels">
<span id="sweet_text"class="left_slider_text">Sweet</span>
<span id="dry_text"class="right_slider_text">Dry</span>
</div>
<input type="range" name="sweet_dry" id="sweet_dry_slider" value="50" min="0" max="100" onchange="sweetDryValue(this.value)" />
<div class="slider_labels">
<span id="crisp_text"class="left_slider_text">Crisp</span>
<span id="buttery_text"class="right_slider_text">buttery</span>
</div>
<input type="range" name="crisp_buttery" id="crisp_buttery_slider" value="50" min="0" max="100" onchange="crispButteryValue(this.value)" />
{% else %}
<div class="slider_labels">
<span id="fruity_text"class="left_slider_text">Fruity</span>
<span id="earthy_text"class="right_slider_text">Earthy</span>
</div>
<input type="range" name="fruity_earthy" id="fruity_earthy_slider" value="50" min="0" max="100" onchange="fruityEarthyValue(this.value)" />
<div class="slider_labels">
<span id="smooth_text" class="left_slider_text">Smooth</span>
<span id="spicy_text" class="right_slider_text">Spicy</span>
</div>
<input type="range" name="smooth_spicy" id="smooth_spicy_slider" value="50" min="0" max="100" onchange="smoothSpicyValue(this.value)" />
{% endif %}
<div class="slider_labels">
<span id="light_text"class="left_slider_text">Light</span>
<span id="robust_text" class="right_slider_text">Robust</span>
</div>
<input type="range" name="light_robust" id="light_robust_slider" value="50" min="0" max="100" onchange="lightRobustValue(this.value)" />
<div class="slider_labels">
<span id="sad" class="left_slider_text">Sad</span>
<span id="rad" class="right_slider_text">Rad</span>
<div id="label_rating">Label Rating</div>
</div>
<input type="range" name="label_rating" id="label_rating_slider" value="50" min="0" max="100" onchange="labelRatingValue(this.value)" />
<br>
<br>
<div class="ui-grid-b">
<div class="ui-block-a">
<input type="radio" name="like_dislike" id="like" value="like" />
<label for="like">like</label>
</div>
<div class="ui-block-b">
<input type="radio" name="like_dislike" id="dislike" value="dislike" />
<label for="dislike">dislike</label>
</div>
<div class="ui-block-c">
<input type="checkbox" name="fave" id="checkbox-1" class="custom" />
<label for="checkbox-1">fave</label>
</div>
</div>
</fieldset>
</div>
<input type="submit" value="Judged!" rel="external"/>
检查我的帖子请求时,我没有传递的唯一内容是:wine_id和user_id。这真令人困惑。
我错过了这么简单吗?
我查看了documentation和示例,但到目前为止没有运气。
答案 0 :(得分:0)
实际上看起来你不需要你的wine_id和user_id(除非你想让用户为其他用户发布评论,或者转换他们正在审阅的葡萄酒)。
这应该适合你:
@login_required
def wine_review_page(request, wine_id):
wine = get_object_or_404(Wine, pk=wine_id)
if Review.objects.filter(user=request.user, wine=wine).exists():
review = Review.objects.get(user=request.user, wine=wine)
else:
review = Review(user=request.user, wine=wine) #this added
if request.method == 'POST':
form = WineReviewForm(request.POST, instance=review)
if form.is_valid():
form.save()
return HttpResponseRedirect('/detail/%s/' % wine_id )
else:
form = WineReviewForm(instance=review)
variables = RequestContext(request, {'form': form, 'wine': wine })
return render_to_response('wine_review_page.html', variables)
如果您 希望允许用户修改其中任何一个字段,请编辑您的问题以显示您的模板或用户如何提交数据,因为您的所有字段都使用hiddenInput小部件,很难说出任何数据如何进入request.POST