Radio Fields with associated data

13 views
Skip to first unread message

st...@bluezilla.com

unread,
Aug 7, 2014, 2:37:55 PM8/7/14
to django...@googlegroups.com
So I have an issue here.  I'm  trying to render a form, a product form with associated credit card collection, etc.  

The problem is this: The client wants the product to be rendered like so:

<radio button>Choice     item price    item setup fee (can be edited)

So I've got a series of products that are output as a choice field using a radio widget.  How do I output the item price as well as a item setup fee for each line?  

My solution right now has been to almost ignore Django's forms and just do it manually--but that means I'm skipping Django's validation and having to do it manually, and trying to output initial values for all three fields on failed validation is baffling me.  

Current template snippet:  
{% for radio in products %}
       
<div  class='row'>
       
<div class='four columns'><p><label>
       
<input  type="radio" name='product' value="{{ radio.item }}" {% if selectedProduct == radio.item %} checked="true" {% endif %}> {{ radio.item }}</label></p></div>
       
<div class="two columns"><p>${{ radio.price }}</p></div>
       
<div class = "six columns"><p>Setup Fee: <input type='text' name="setup-{{ radio.item }}" value="{{ radio.setup_fee }}"></p></div>
       
</div>
{% endfor %}


View Code (slightly edited)
category = Product.objects.filter(category__name="bubba")
    products
= ProductItem.objects.filter(product=category)
    products
= products.order_by('pk')
    selectedProduct
= False
   
if request.method == 'POST':  # If the form has been submitted...
       
        bubba
= bubbaForm(request.POST)
        payment
= PaymentForm(request.POST)  # A form bound to the POST data
       
if 'product' in request.POST:
            validProduct
= True
            selectedProduct
= request.POST['product']
       
else:
            validProduct
= False
       
# print bubba
       
print request.POST
       
# All validation rules pass
       
if bubba.is_valid() and payment.is_valid() and validProduct:
           
# Process the data in form.cleaned_data
           
# print "in post"
           
from datetime import date
           
from django.template.defaultfilters import slugify
            cart
= Cart()
            cart
.client_email = bubba.cleaned_data['client_email']
            cart
.client_name = bubba.cleaned_data['client_name']
            cart
.client_phone = bubba.cleaned_data['client_phone']
            cart
.salesman = request.user
            product
= InvoiceItem()
            setup
= InvoiceItem()
            item
= ProductItem.objects.get(item=request.POST['product'])
            product
.item = item
            product
.price = item.price
            product
.description = "The product the client selected"
            product
.slug = slugify(product.item)
            product
.recurring = True
            product
.recurring_interval = "monthly"
            product
.recurring_start_date = date.today()


            setupName
= 'setup-' + request.POST['product']


            setup
.item = "Setup Fee"
            setup
.price = Decimal(request.POST[setupName])  # item.setup_fee
            setup
.description = "Setup Fee"
            setup
.slug = "setup-fee"
            setup
.recurring = False


            cart
.order_total = product.price + setup.price


           
# print bubba.cleaned_data
            ccData
= dict(bubba.cleaned_data.items() + payment.cleaned_data.items())
            ccData
['product'] = request.POST['product']
            ccData
['price'] = item.price
            ccData
['setup_fee'] = setup.price
           
print ccData
            processCC
(request, ccData)

# this code relies on responses from processCC:
           
if request.session.get('transaction_status') != "Approved":
               
return HttpResponseRedirect('/sales/bubba/error/')
           
else:
               
return HttpResponseRedirect('/sales/bubba/thanks/')  # Redirect after POST
       
else:  # we didn't pass validation, so we need to rebind products
           
pass
   
else:
        bubba
= bubbaForm()  # An unbound form
        payment
= PaymentForm()


    data
= {'bubba': bubba, 'payment': payment, 'products': products,
           
'selectedProduct': selectedProduct}
   
# print 'selectedProduct' + str(selectedProduct)


   
return render(request, "sales/salesCart.html", data)

Collin Anderson

unread,
Aug 7, 2014, 9:41:25 PM8/7/14
to django...@googlegroups.com
If nothing else, you can use a form in the view for validation, but still render the radio inputs completely manually.
Reply all
Reply to author
Forward
0 new messages