How to access and make use of html data in django? -


i having hard time figuring out right logic problem, have 3 models,

class item(smartmodel):      name= models.charfield(max_length=64,help_text="name item e.g hamburger")      price=models.decimalfield(max_digits=9,decimal_places=2)     optionalitems = models.manytomanyfield('optionalitems.optionalitemcategory',null=true,blank=true)  class optionalitems(smartmodel):      """optional items belong item e.g topping belongs pizza"""       name = models.charfield(max_length=20, help_text="item name.")      price = models.decimalfield(max_digits=9, decimal_places=2, null=true,blank=true)  class optionalitemcategory(smartmodel):     """category optional items belong"""      title = models.charfield(max_length=20,help_text="category name")     optional_items = models.manytomanyfield(optionalitems) 

in template,

{%for optionalcategory in optionalcategories %}   <h5 id="blah"> {{ optionalcategory.title}} </h5>   {% optionalitem in optionalcategory.optional_items.all %}    <ul>     <input type="radio" value="radio" name="optional" value="op"><li id="item_appearence">{{optionalitem.name}}<span> {{optionalitem.price}}</span></li><a/>    </ul>   {% endfor %}  {% endfor %} 

so example item burrito have optionalitem steak or chicken.i able access item item = get_object_or_404(item, pk=obj.id) problem cannot figure out how capture optionalitem. want able access optionalitem, want obtain value of radio button , attributes. kind of tricky.

your code inconsistent , makes hard read, understand , work with. advice, clean up. this:

class category(models.model):     name = models.charfield(max_length=200)  class option(models.model):     name = models.charfield(max_length=200)     price = models.decimalfield(max_digits=9, decimal_places=2, blank=true, null=true)  class item(models.model):     category = models.foreignkey(category)     name = models.charfield(max_length=200)     price = models.decimalfield(max_digits=9, decimal_places=2, blank=true, null=true)     options = models.manytomanyfield(option) 

than need , view. interpret question: want form select option item. code below render options , widget radioselect() lets user select 1 item. why use radiobuttons? if item has relation 1 option, item model should have foreignkey not m2m!

form:

class itemform(modelform):     options = forms.choicefield(widget=forms.radioselect())     class meta:         model = item         fields = ( 'options', ) 

view:

from django.shortcuts import render django.http import httpresponseredirect

def your_view(request, id):     item = item.objects.get(pk=id) # item object.     if request.method == 'post': # if form has been submitted...         form = contactform(request.post) # form bound post data         if form.is_valid(): # validation rules pass             # process data in form.cleaned_data             options = form.cleaned_data['options']             # ...             return httpresponseredirect('/thanks/') # redirect after post     else:         form = articleform(instance=article)      return render(request, 'contact.html', {         'form': form,     }) 

template:

{% obj in item.options_set.all %}     {{ obj.name }} {{ obj.price }} {% endfor %}  <form action="" method="post">{% csrf_token %}      {{ form.as_p }} <input type="submit" value="submit" /> </form> 

i dind't test code. should started. documentation friend: https://docs.djangoproject.com/en/1.5/topics/forms/ https://docs.djangoproject.com/en/1.5/#forms


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -