Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <input id="id_product" type="text" name="product" value="aassddf" maxlength="250" />
- FAIL
- from django.db import models
- from django.forms import ModelForm
- class Category(models.Model):
- name = models.CharField(max_length=250)
- def __unicode__(self):
- return self.name
- class Product(models.Model):
- category = models.ForeignKey(Category)
- product = models.CharField(max_length=250)
- quantity = models.IntegerField(default=0)
- price = models.FloatField(default=0.0)
- def __unicode__(self):
- return self.product
- class ProductForm(ModelForm):
- class Meta:
- model = Product
- from models import *
- from django.shortcuts import render_to_response
- from django.http import HttpResponseRedirect
- def index(request):
- ...
- ...
- def add_product(request):
- if request.method == 'POST':
- form = ProductForm(request.POST)
- print form['product']
- if form.is_valid():
- form.save()
- return HttpResponseRedirect('/product')
- else:
- print 'FAIL'
- return HttpResponseRedirect('/product')
- <form method="post" action="add_product/">
- {% csrf_token %}
- <label for="category">Category</label>
- <select name="category" id="category">
- {% for category in category_list %}
- <option> {{ category.name }} </option>
- {% endfor %}
- </select>
- <label for="product">Product</label>
- <input type="text" name="product" id="product">
- <label for="quantity">Quantitiy</label>
- <input type="text" name="quantity" id="quantity">
- <label for="price">Price</label>
- <input type="text" name="price" id="price">
- <input type="submit" value="Add New product" id="create">
- </form>
- <form method="post" action="add_product/">
- {% csrf_token %}
- {{ form.as_p }}
- </form>
- class ProductForm(ModelForm):
- class Meta:
- model = Product
- from django import forms
- from models import Category
- class ProductForm(forms.Form):
- # Put all my Categories into a select option
- category = forms.ModelChoiceField(queryset=Category.objects.all())
- product = forms.CharField()
- quantity = forms.IntegerField()
- price = forms.FloatField()
- def add_product(request):
- success = False
- if request.method == "POST":
- product_form = ProductForm(request.POST)
- if product_form.is_valid():
- success = True
- category = Category.objects.get(name=product_form.cleaned_data['category'])
- product = product_form.cleaned_data['product']
- quantity = product_form.cleaned_data['quantity']
- price = product_form.cleaned_data['price']
- new_product = Product(category = category, product = product, quantity = quantity, price = price )
- new_product.save()
- new_product_form = ProductForm()
- ctx2 = {'success':success, 'product_form':new_product_form}
- return render_to_response('product/add_product.html', ctx2 , context_instance=RequestContext(request))
- else:
- product_form = ProductForm()
- ctx = {'product_form':product_form}
- return render_to_response('product/add_product.html', ctx , context_instance=RequestContext(request))
- {% if success %}
- <h3> product added successfully </h3>
- {% endif %}
- <form method="post" action=".">
- {% csrf_token %}
- {{ product_form.as_p }}
- <input type="submit" value="Add New product" id="create">
- <input type="reset" value="reset" id="reset">
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement