0

Hello Everyone.

Creating form from model. Faced with the problem that I get a raw form to which I can't add css styles to every FormModel field (div blocks with a class). Question: Are there any tools that allow you to style a template based on ModelForm? (I have css and the desired template-html)

model.py

class Product(models.Model):
    name = models.CharField(max_length=40)
    
    def __str__(self):
        return self.name

class Variety(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    name = models.CharField(max_length=40)

    def __str__(self):
        return self.name

class Category(models.Model):
    name = models.CharField(max_length=40)

    def __str__(self):
        return self.name

class Tender(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    variety = models.ForeignKey(Variety, on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=4, decimal_places=2,)
    delivery_date = models.DateField(default=date.today)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    delivery_basis = models.CharField(max_length=3, choices=[('CPT', 'CPT')])
    payments = models.CharField(max_length=20, choices=[('Наличный расчет', 'Наличный расчет')])
    comment = models.TextField(max_length=4000, blank=True, default='Комментарий отсутствует.')

    def __str__(self):
        return self.product

forms.py

class AddTenderForm(forms.ModelForm):
    class Meta:
        model = Tender
        fields = '__all__'

views.py

def index(request):
    form = AddTenderForm()
    if request.method == 'POST':
        form = AddTenderForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('add')
    return render(request, 'fmtender/form_for_adding_a_tender.html', {'form': form})

form_for_adding_a_tender.html

<form class="needs-validation"
      id="AddTender"
      data-variety-url="{% url 'ajax_load_variety' %}"
      novalidate>
    {% csrf_token %}
    {{ form.as_p }}
    <button class="w-100 btn btn-primary btn-lg"
            type="submit">Continue to checkout</button>
</form>

how it turns out

the way I would like

  • `Error in {% for x` which error message? Anyways `AddTenderForm` is a simple ModelForm for `Tender` model which contains `product = models.ForeignKey` which means that `product` is a scalar value thus cannot be iterated. Describe _what_ you're trying to implement, not _how_. – Ivan Starostin Jun 18 '23 at 09:15
  • @IvanStarostin **What you're trying to implement?** I copied the html code of the [bootstrap form](https://getbootstrap.com/docs/5.3/examples/checkout/) and trying to fill it with data from forms.ModelForm. **Which error message?** Just like you said: 'ModelChoiceField' object is not iterable – DmitriPavlyuchenko Jun 18 '23 at 10:15
  • And what functionality are you trying to implement, which "business value" are you trying to deliver by copying forms, by writing for loops? Are you asking how to implement a form with a product select dropdown menu? Please edit your question to make clearer. And put error message to the question, not to comments. – Ivan Starostin Jun 18 '23 at 10:35
  • @IvanStarostin clarified the essence of the question. I hope it is more clear to you and you can help me – DmitriPavlyuchenko Jun 18 '23 at 12:28
  • Check out this answer https://stackoverflow.com/questions/5827590/css-styling-in-django-forms – Ivan Starostin Jun 19 '23 at 06:21
  • You can add custom element class by specifying widget and passing `attrs` parameter when declaring `ModelForm`. However, I would argue doing this makes your frontend hard to maintain, and suggest you render form element manually. – Ezon Zhao Jun 20 '23 at 01:57

0 Answers0