A
A
alexkomp2019-07-18 17:17:05
Django
alexkomp, 2019-07-18 17:17:05

How to put a model in a form in Django?

It is necessary that the user can fill out the form on the site, and not in the admin panel.
There is this model:

class Product(models.Model):
    title = models.CharField(max_length = 130)
    slug = models.SlugField()
    description = models.TextField()
    image = models.ImageField()
    prise = models.DecimalField(max_digits = 7, decimal_places=0)
    available = models.BooleanField(default=True)
    
    
    def __str__(self):
        return self.title

views.py
def create_product(request):
    form = ProductForm(request.POST)
    if form.is_valid():
        company = form.save()
        return redirect(company)
    else:
        return render(request, 'product_form.html', {'form': form})

and template
<form method="POST" action="/" > {% csrf_token %}
    {{ form }}
    <input type="submit" value="Добавить" >
</form>

How to put the model in the form?
And how to display the form on the site?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-07-18
@alexkomp

Have you looked here and here ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question