23đź‘Ť
That’s because you’re not “feeding” your form.
Do this:
invoicing_data_form = InvoicingDataForm(instance=invoice, data=request.POST or None)
11đź‘Ť
You have an unbound form.
https://docs.djangoproject.com/en/1.7/ref/forms/api/#bound-and-unbound-forms
A Form instance is either bound to a set of data, or unbound.
If it’s bound to a set of data, it’s capable of validating that data and rendering the form as HTML with the data displayed in the HTML.
If it’s unbound, it cannot do validation (because there’s no data to validate!), but it can still render the blank form as HTML.
To bind data to a form, pass the data as a dictionary as the first parameter to your Form class constructor:
invoicing_data_form = InvoicingDataForm(request.POST or None, instance=invoice)
2đź‘Ť
If you’re already giving request.POST to your form using request.POST or None
, but it’s still invalid without errors, check that there isn’t any redirect going on. A redirect loses your POST data and your form will be invalid with no errors because it’s unbound.
- Why can't I upload jpg files to my Django app via admin/?
- Django-allauth: how to properly use email_confirmed signal to set user to active
- Pycharm Can't retrieve image ID from build stream
- How can my Model primary key start with a specific number?
- VS Code: Python Interpreter can't find my venv
1đź‘Ť
I got this for AuthenticationForm
which needs AuthenticationForm(None, request.POST)
see Using AuthenticationForm in Django
- Why use Django's collectstatic instead of just serving the files directly from your static directory?
- Django template img src not working
- Django logging requests
- Disable pylint warning E1101 when using enums
- Accessing Django OneToOneField in templates?
0đź‘Ť
I want to expand on the answer by @yuji-tomita-tomita
I typically use a CBV approach in Django, and how I’m handling forms:
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
# do things
Reading the source code I noticed that self.get_form()
using get_form_kwargs(self)
to populate the form with request.POST
, thus getting bound to data. So if you’re overloading it like I did:
def get_form_kwargs(self):
company = self.get_company()
return {"company": company}
Make sure to call the super()
, and it will finally work:
def get_form_kwargs(self):
company = self.get_company()
kwargs = super().get_form_kwargs()
kwargs.update({"company": company})
return kwargs
- Python Social Auth Django template example
- Get a list of python packages used by a Django Project
- Python-social-auth with Django: ImportError: No module named 'social_django'
- Django DB level default value for a column
- Sending a message to a single user using django-channels