0

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.

Community
  • 1
  • 1
bmartinek
  • 568
  • 2
  • 6
  • 17
  • you have some indentation problems in `WineReviewForm` `Meta` class or is it only here? – manji Apr 08 '11 at 16:24
  • Can you post your entire modelform? When i showed my example, I excluded those 2 fields by leaving them off the meta "fields" attribute because you were explicitly setting those. Make sure that's not the issue. – dting Apr 08 '11 at 17:56
  • @manji sorry that was just on SO, fixed and added whol modelForm. – bmartinek Apr 08 '11 at 18:44
  • @kriegar - I didn't add the add the fields attribute to the model form above. I thought that it it includes everything unless you are doing a partial form: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form – bmartinek Apr 08 '11 at 18:47
  • You are correct. Your code is redacted so I couldn't tell if you had just left that off your question or if you hadn't used it. – dting Apr 08 '11 at 18:50

1 Answers1

0

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

dting
  • 38,604
  • 10
  • 95
  • 114
  • Thanks again for the assistance, I owe you a beer next time i am in SJ. Now I am able to change reviews, but a second user can't review the wine. `error: Review with this Wine already exists.` Will include the form template, its a bit lonmg though... – bmartinek Apr 08 '11 at 19:50
  • Okay posted form above, SO formatting is still a bit new to me. Hopefully its slightly readable. – bmartinek Apr 08 '11 at 19:56
  • @bmartinek I think that's caused by an error in your model? Do you have the attribute `unique=True` for your `wine` field? You might want to look at unique together Meta attribute, http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together, instead of setting wine as unique as that would cause an error regardless of the user changing. Ahh i just looked at your first question, that is definitely the problem. Remove `unique=True` for the user field as well. – dting Apr 08 '11 at 21:46
  • GAH! That was it. Too funny I removed it, but forgot to restart the app. Thanks for your help setting the User and wine! Make that beer a 6-pack. – bmartinek Apr 08 '11 at 22:34