I need to send form data via POST request to save a new model instance to DB. I have a hidden field name="owner" in html which has a value of authorized user id. I can see this value in HTML code, but not in POST request.
This gives me Chrome Devtools:
<input type="hidden" id="owner" name="owner" value="3">
And this gives me a pyCharm's debugger:
cleaned_data = {'latitude': 55.2288, 'longitude': 24.3686, 'comment': '', 'category': <Category: Host>}
What is the problem?
I have this code in Django views.py:
if request.method == "POST":
form = AddMarkerForm(request.POST)
if form.is_valid():
form.save()
messages.success(
request, _("Marker added successfully!"), extra_tags="success"
)
return redirect(to="home")
else:
messages.error(request, _("Error. Check coordinates."), extra_tags="danger")
else:
form = AddMarkerForm()
And html:
<form class="row g-3" method="post" enctype="multipart/form-data"
id="addMarkerForm" name="addMarkerForm">
{% csrf_token %}
{% for field in form.visible_fields|slice:":2" %}
<div class="col-md-6">
{{ field }}
</div>
{% endfor %}
{% for field in form.visible_fields|slice:"2:" %}
<div class="col-12">
{{ field }}
</div>
{% endfor %}
<input type="hidden" id="owner" name="owner"
value="{% if request.user %}{{ request.user.id }}{% endif %}">
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-bs-dismiss="modal">{% translate "Close" %}</button>
<button type="submit" name="submit" class="btn btn-primary">{% translate "Add" %}</button>
</div>
</form>
A form code in forms.py:
class AddMarkerForm(forms.ModelForm):
"""Form for adding a new marker on a frontend."""
latitude = forms.FloatField(
widget=forms.TextInput(
attrs={
"class": "form-control",
"id": "latitude",
"placeholder": _("Latitude"),
}
),
)
longitude = forms.FloatField(
widget=forms.TextInput(
attrs={
"class": "form-control",
"id": "longitude",
"placeholder": _("Longitude"),
}
)
)
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
empty_label=_("Choose category"),
widget=forms.Select(attrs={"class": "form-select"}),
)
class Meta:
model = Marker
fields = ["latitude", "longitude", "comment", "category"]
widgets = {
"comment": forms.Textarea(
attrs={
"class": "form-control",
"rows": 3,
"max-length": 200,
"placeholder": _("Comment, not required"),
}
),
}