1. <input id="id_product" type="text" name="product" value="aassddf" maxlength="250" />
  2. FAIL
  3.  
  4. from django.db import models
  5. from django.forms import ModelForm
  6.  
  7. class Category(models.Model):
  8. name = models.CharField(max_length=250)
  9.  
  10. def __unicode__(self):
  11. return self.name
  12.  
  13. class Product(models.Model):
  14. category = models.ForeignKey(Category)
  15. product = models.CharField(max_length=250)
  16. quantity = models.IntegerField(default=0)
  17. price = models.FloatField(default=0.0)
  18.  
  19. def __unicode__(self):
  20. return self.product
  21.  
  22. class ProductForm(ModelForm):
  23. class Meta:
  24. model = Product
  25.  
  26. from models import *
  27. from django.shortcuts import render_to_response
  28. from django.http import HttpResponseRedirect
  29.  
  30. def index(request):
  31. ...
  32. ...
  33.  
  34. def add_product(request):
  35. if request.method == 'POST':
  36. form = ProductForm(request.POST)
  37. print form['product']
  38. if form.is_valid():
  39. form.save()
  40. return HttpResponseRedirect('/product')
  41. else:
  42. print 'FAIL'
  43. return HttpResponseRedirect('/product')
  44.  
  45. <form method="post" action="add_product/">
  46. {% csrf_token %}
  47. <label for="category">Category</label>
  48. <select name="category" id="category">
  49. {% for category in category_list %}
  50. <option> {{ category.name }} </option>
  51. {% endfor %}
  52. </select>
  53.  
  54. <label for="product">Product</label>
  55. <input type="text" name="product" id="product">
  56.  
  57. <label for="quantity">Quantitiy</label>
  58. <input type="text" name="quantity" id="quantity">
  59.  
  60. <label for="price">Price</label>
  61. <input type="text" name="price" id="price">
  62.  
  63. <input type="submit" value="Add New product" id="create">
  64. </form>
  65.  
  66. <form method="post" action="add_product/">
  67. {% csrf_token %}
  68. {{ form.as_p }}
  69. </form>
  70.  
  71. class ProductForm(ModelForm):
  72. class Meta:
  73. model = Product
  74.  
  75. from django import forms
  76. from models import Category
  77.  
  78. class ProductForm(forms.Form):
  79. # Put all my Categories into a select option
  80. category = forms.ModelChoiceField(queryset=Category.objects.all())
  81. product = forms.CharField()
  82. quantity = forms.IntegerField()
  83. price = forms.FloatField()
  84.  
  85. def add_product(request):
  86. success = False
  87.  
  88. if request.method == "POST":
  89. product_form = ProductForm(request.POST)
  90.  
  91. if product_form.is_valid():
  92. success = True
  93.  
  94. category = Category.objects.get(name=product_form.cleaned_data['category'])
  95. product = product_form.cleaned_data['product']
  96. quantity = product_form.cleaned_data['quantity']
  97. price = product_form.cleaned_data['price']
  98.  
  99. new_product = Product(category = category, product = product, quantity = quantity, price = price )
  100. new_product.save()
  101.  
  102. new_product_form = ProductForm()
  103.  
  104. ctx2 = {'success':success, 'product_form':new_product_form}
  105. return render_to_response('product/add_product.html', ctx2 , context_instance=RequestContext(request))
  106. else:
  107. product_form = ProductForm()
  108. ctx = {'product_form':product_form}
  109. return render_to_response('product/add_product.html', ctx , context_instance=RequestContext(request))
  110.  
  111. {% if success %}
  112. <h3> product added successfully </h3>
  113. {% endif %}
  114. <form method="post" action=".">
  115. {% csrf_token %}
  116.  
  117. {{ product_form.as_p }}
  118.  
  119. <input type="submit" value="Add New product" id="create">
  120. <input type="reset" value="reset" id="reset">
  121. </form>