I'm trying to add a form on my page that will update a certain field in my DB.
This is what the template looks like:
{% for row in mydb %}
<form action="/myview/{{myslug}}/" method="post" novalidate>
{% csrf_token %}
{% include 'main/includes/bs4_form.html' with form=form3 %}
{{row.firstfield}}
{{row.secondfield}}
<button name="button3" type="submit" class="btn btn-danger style="background-color: red;">CANCEL</button></td>
</form>
{% endfor %}
The for statements is to display each row of the data in my table in a single line. Near each line, there is a submit button. When this button is clicked, the field secondfield is updated with the value New Value, as you can see in my form.
Thi is the part of the view involved:
def myview(request, tvticker):
row = mydb.objects
if request.method == 'POST':
...
if 'button3' in request.POST:
form3 = form = UpdateForm(request.POST)
if form3.is_valid():
profile = form.save(commit=False)
profile.save()
messages.success(request, f"Success")
...
render(request, "main/mytemplate.html",
context={'row': row,})
And here is the form:
class UpdateForm(forms.ModelForm):
SomeField = forms.CharField()
class Meta:
model = MyModel
fields = ("SomeField",)
def save(self, commit=True):
send = super(UpdateForm, self).save(commit=False)
send.secondfield = 'New Value'
if commit:
send.save()
return send
My actual code it's not working, is seems to send a POST request but the secondfield value is not updated. For example, if i click the submit button of the second record in my template.html, the secondfield value of that record should be changed from oldvalue to newvalue. At the moment, the value is not updated.
Any advice is appreciated, thanks in advance!
actionin the form?<form action="/myview/" method="post" novalidate>?form3.is_valid()successful ?python manage.py shelland try to update using command line. then you can isolate the problem to form, view or html. Also check what data is going in post request using chrome dev tools (enablepreserve login network to see the data after page change)