Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango forms validation on the wrong object?
    text
    copied!<p>I am trying to edit some old code someone had created, it's a mini e-commerce type page with products. The idea is that if someone add's a product to their cart they can't add the same one again, there is no quantity involved. </p> <p>You'll notice by the code below that the previous guy had set a session for each one of those items. I want to make sure that the user cannot select the same item twice so I figure testing to see if the session already exists and raise a forms validation error if the session does exist is the way to go. The below code does not work, the problem is that it seems to throw the ValidationError when it finds the first True condition so none of the other conditions are even checked. For example - If I add "home_options" in my cart already, and then select "advanced_options", which does not exist in my cart, it raises forms.ValidationError even if "advanced_options" is not in my cart, I had an "or" inbetween and that did not work either, any suggestions?</p> <pre><code>def clean(self): err = _("You already have this item in your cart.") if self.request: try: if self.request.session['home_options']: raise forms.ValidationError(err) if self.request.session['value_home_options']: raise forms.ValidationError(err) if self.request.session['super_options']: raise forms.ValidationError(err) if self.request.session['advanced_options']: raise forms.ValidationError(err) if self.request.session['advanced_highspeed_options']: raise forms.ValidationError(err) except KeyError: pass # only gets here if the session does not exist, add the item to the cart in this case. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload