开发者

ModelForm not passing all parameters

开发者 https://www.devze.com 2023-02-22 21:32 出处:网络
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 :-(.

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消