I am getting Method Not Allowed (POST) when I send an ajax post. It doesn't even get to the first request.method == 'POST' validation.
I already checked everything I could think of and still I cannot figure out the error, probably something ridiculous. Already tried submitting the form directly through the 'action' attribute pointing to the same url, with the 'POST' method and I get the same error.
Any thoughts/suggestions?
My url:
url(r'^crear_familiar/$', familiar_create, name='crear_familiar'),
My ajax:
var url = "{% url 'crear_familiar' %}";
$.ajax({
type: "POST",
url: url,
data: $("#familiarForm").serialize(),
success: function (data) {
console.log(data, 'SUCCESS');
location.reload();
},
error: function (data) {
console.log('ERROR', data);
}
});
My view:
def familiar_create(request):
if request.method == 'POST':
familiar_form = FamiliarForm(request.POST, prefix='familiar')
if familiar_form.is_valid():
familiar = familiar_form.save(commit=False)
familiar.save()
message = 'Has creado un nuevo familiar'
if request.POST['familiar_familia']:
familia_id = request.POST['familiar_familia']
familia = Familia.objects.get(id=familia_id)
tipo_familiar_id = request.POST['fhf-tipo_familiar']
tipo_familiar = TipoFamiliar.objects.get(id=tipo_familiar_id)
familiar_assignment = FamiliaHasFamiliar(
familia=familia,
familiar=familiar,
tipo_familiar=tipo_familiar
)
familiar_assignment.save()
message = 'Has creado un nuevo familiar y le has asignado una familia'
status = 200
else:
message = 'Ocurrió un error al crear al familiar'
status = 500
return JsonResponse({'status': 'false', 'message': message},
status=status)
else:
print 'GET'
return JsonResponse(
{'status': 'false', 'message': 'Wrong request'},
status=500
)
Form:
<form id="familiarForm" method="POST" action="{% url 'crear_familiar' %}">
{% csrf_token %}
<input name="familiar_familia" value="{{ object.pk }}" hidden>
<div class="row">
<div class="col-md-6">
<div class="form-group">
{{ familiar_form.nombre.label }}:
<span class="text-danger bold">*</span>
{{ familiar_form.nombre|add_class:"form-control required" }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{{ familiar_form.apellidos.label }}:
<span class="text-danger bold">*</span>
{{ familiar_form.apellidos|add_class:"form-control required" }}
</div>
</div>
</div>
</form>
<div class="text-right">
<button id="saveFamiliar" type="submit" class="btn btn-primary" familiar_pk="0">
Submit <i class="icon-arrow-right14 position-right"></i>
</button>
</div>
Finally found the error: Right before the url that was not working I had another one with a regex:
url(r'^(?P<pk>[-\w]+)/$', FamiliaDetailView.as_view(),
name='detalle_familia')
Looks like this one acted up when I sent the one I actually needed but thought I was sending the wrong arguments/method. I thought it was actually using the one I needed because the request contained the 'correct' text for the url.
The fix was to modify it so the regex did not match any others after it:
url(r'^detalle/(?P<pk>[-\w]+)/$', FamiliaDetailView.as_view(),
name='detalle_familia')