Struggling with this issue for a few days now so any help is greatly appreciated.
I managed to deploy my django project on a Linux server and it works fine apart from the model form submit. On the local development server it work fine and here is the workflow:
- User fills the form;
- User clicks submit;
- Javascript catches the submit event, submits a “POST” request and gets a response back;
- On the server side, the form is checked and if all is good an email is sent;
- HTML is updated to add the confirmation of form registration;
Here is my code:
home.html
<div class="col-lg-8 offset-lg-2">
<form method="POST" class="row mt-17" id="form" onsubmit="return false">
{% csrf_token %}
{% for field in form %}
{% if field.name not in "message" %}
<div class="col-12 col-sm-6">
<div class=form-group>
<label for={{field.name}} class="form-label">{{ field.label }}</label>
{{ field }}
</div>
</div>
{% else %}
<div class="col-12">
<div class=form-group>
<label for={{field.name}} class="form-label">{{ field.label }}</label>
{{ field }}
</div>
</div>
{% endif %}
{% endfor %}
<div class="form-group mb-0">
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</form>
</div>
main.js
const form = document.getElementById('form');
form.addEventListener("submit", submitHandler);
function submitHandler(e) {
e.preventDefault();
fetch("{% url 'messages-api' %}", {
credentials: "include",
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new FormData(form)
})
.then(response => response.json())
.then(data => {
alert("Got your message")
})
}
ssl.conf file
<Directory /home/admin/pinpoint/templates>
Require all granted
</Directory>
Alias /static /home/admin/pinpoint/static
<Directory /home/admin/pinpoint/static>
Require all granted
</Directory>
Alias /media /home/admin/pinpoint/media
<Directory /home/admin/pinpoint/media>
Require all granted
</Directory>
<Directory /home/admin/pinpoint/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/admin/pinpoint/project/wsgi.py
WSGIDaemonProcess pinpoint python-path=/home/admin/pinpoint python-home=/home/admin/pinpoint/venv
WSGIProcessGroup pinpoint
On the development server, whenever you click submit, I get 403 (Forbidden) in the console.
Since the development version works fine, my guess would be it’s a permission issue. As such, I gave apache ownership and r+w rights in my templates folder. Still the issue persists.
If i remove the headers content in the fetch command, the form is registered in the database but after ~2 minutes I get Server Error(500).
Any help/suggestions are welcome.