ModelForm 未传递所有参数

发布于 2024-10-31 18:39:39 字数 4921 浏览 1 评论 0原文

经过一些 @kreigar 的大力帮助 我能够开始使用 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。这确实令人困惑。

我错过了一些简单的事情吗?

我查看了 文档 和示例,但到目前为止没有运气。

After some great help by @kreigar I was able to start using django's ModelForm instead of form.Forms. Only problem now is that I am not passing the two primary keys to associate my review form :-(.

Currently my view looks like such:

 #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)`

and my model form like this:

 #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"/>

When checking my post request the only things I am not passing are the: wine_id and the user_id. This is really confusing.

Am I missing soomething simple?

I looked through documentation and examples but no luck so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2024-11-07 18:39:40

实际上看起来您不需要 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小部件,很难判断数据如何进入请求。POST

It actually looks like you wouldn't need your wine_id and user_id (unless you want to allow users to post reviews for other users, or switch wines they are reviewing on the fly).

This should work for you:

@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)

If you do want to allow the user to modify either of those fields, edit your question to show your template or how the users are submitting data, since all your fields are using hiddenInput widgets, Its hard to tell how any data would get into the request.POST

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文