I am encountering an issue with sending a post request to the server. The request.POST on the server seems to be empty for some unknown reason. Below are examples of the code:
Front-end JavaScript:
let weekends = []
await fetch('{% url "get_weekends" %}', {
method: 'POST', headers: new Headers({'Accept': 'application/json', 'Content-Type': 'application/json'}),
params: {after: after_str, before: before_str}
}).then(function (response) {
return response.json();
}).then(function (data) {
if (data['status'] === 'ok') {
weekends = data['weekends']
} else {
console.error(data)
show_error('Failed to retrieve your weekends list! Error in status!')
}
}).catch(function (e) {
console.error(e)
show_error('Failed to retrieve your weekends list! Request failed!')
});
Values of after_str and before_str variables hold string data (01.12.2021 and 31.12.2021).
Back-end Python:
def get_weekends_view(request):
""" This view returns a user's weekend list within a specified time frame """
form = GetAfterAndBeforeForm(request.POST)
if form.is_valid():
after, before = form.cleaned_data['after'], form.cleaned_data['before']
result = []
for weekend in Weekend.objects.filter(user=request.user, date__range=(after, before)):
result.append({'date': weekend.date.strftime('%d.%m.%Y'), 'status': weekend.get_status_display()})
return {'status': 'ok', 'weekends': result}
else:
return {'status': 'error', 'msg': 'Form filled incorrectly! ' + str(request.POST)}
Forms:
class GetAfterAndBeforeForm(forms.Form):
after = forms.DateField()
before = forms.DateField()