Some lessons about templates with django
On Mon, Jul 1, 2019 at 8:29 AM Joe Reitman <[email protected]> wrote: > Try this: > > > def sell(request): > if request.method == 'POST': > form = ProductsForm(request.POST) > if form.is_valid(): > print('form is valid') > form.save() > return redirect('home') > > else: > form = ProductsForm() > return render(request, 'sell/products_form.html', {'form': form}) > > > On Monday, July 1, 2019 at 6:35:31 AM UTC-5, brian wrote: >> >> model >> >> class Products(models.Model): >> >> UNIT = ( >> ('whole', 'whole unit'), >> ('item', 'per single item'), >> ) >> >> >> # category = models.ForeignKey(Category, related_name='products', >> on_delete=models.CASCADE) >> ProductName = models.CharField(max_length=255) >> user = models.ForeignKey(User, on_delete=models.CASCADE) >> ProductDescription = models.TextField(max_length=500, blank=True) >> price = models.FloatField() >> location = models.CharField(choices = COUNTIES, max_length=300) >> # category = models.CharField( choices = CATEGORIES, max_length=10, >> default='other') >> category = models.ForeignKey(Category, related_name='products', >> on_delete=models.CASCADE) >> >> unitofsale = models.CharField(max_length=10, choices=UNIT) >> image = models.FileField(upload_to='products_images/', blank=True) >> sublocation = models.CharField(max_length=100) >> created= models.DateTimeField(auto_now_add=True) >> # slug = models.SlugField(max_length=200,db_index=True) >> >> class Meta: >> ordering = ('-created',) >> # index_together = (('id', 'slug'),) >> >> view >> >> def sell(request): >> if request.method == 'POST': >> form = ProductsForm() >> form = ProductsForm(request.POST, request.FILES, instance = >> request.user) >> if form.is_valid(): >> print('form is valid') >> form = form.save(commit=True) >> user = request.user >> form.user = user >> form.save() >> return redirect('home') >> >> else: >> form = ProductsForm() >> return render(request, 'sell/products_form.html', {'form': form}) >> >> template >> >> <form method="POST" action="{% url 'sell' %}" >> enctype="multipart/form-data"> >> {% csrf_token %} >> <div class="row col-md-10"> >> <div class="col-md-6"> >> <div class="form-group"> >> <label>Product Name</label> >> {{ form.ProductName}} >> >> </div> >> >> >> </div> >> <div class="col-md-6"> >> <div class="form-group"> >> <label>Product Price</label> >> {{ form.price }} >> </div> >> </div> >> </div> >> <div class="row col-md-10"> >> <div class="col-md-6"> >> <div class="form-group"> >> <label>County</label> >> {{ form.location}} >> >> </div> >> >> >> </div> >> <div class="col-md-6"> >> <div class="form-group"> >> <label>Sub-location</label> >> {{ form.sublocation }} >> </div> >> </div> >> </div> >> <div class="row col-md-10"> >> <div class="col-md-6"> >> <div class="form-group"> >> <label>Category</label> >> {{ form.category}} >> >> </div> >> >> >> </div> >> <div class="col-md-6"> >> <div class="form-group"> >> <label>Unit of Sale</label> >> {{ form.unitofsale }} >> </div> >> </div> >> </div> >> <div class="row col-md-10"> >> <div class="col-md-6"> >> <div class="form-group"> >> <label>Product Description</label> >> {{ form.ProductDescription}} >> >> </div> >> >> >> </div> >> <div class="col-md-6"> >> <div class="form-group"> >> <label>Upload Product Image</label> >> {{ form.image }} >> </div> >> </div> >> </div> >> <div class="row col-md-10"> >> <div class="col-md-6"> >> <div class="form-group"> >> <button type="submit" class="btn btn-success >> btn-lg mt-5"> Sell </button> >> >> </div> >> >> >> </div> >> </div> >> >> >> <br> >> >> {# <input type="submit" value="Sell">#} >> </form> >> >> >> -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/0066c21c-e4c4-43ed-9314-05ec360bbac9%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/0066c21c-e4c4-43ed-9314-05ec360bbac9%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKXkcrdzQhAj--ua6ROCk9r8Zyx7hsvuk8OzoW%2BWzd_8q5sdGg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

