I have this issues with Django forms where on each refresh the form would resubmit the last input into the database I tried working with "HttpResponseRedirect" but it would either crash the site or show a blank page.
Heres the code of views.py:
def page(request) :
employe = Employe.objects.all()
if request.method =='POST' :
form = EmployeForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect("")
else :
form = EmployeForm()
return render(request,'hello.html',{'employe':employe,'form':form,})
Form template:
<form method="POST">
{% csrf_token %}
{{ form }}
<button type="submit" >Ajouter</button>
</form>
Forms.py:
from django import forms
from .models import Employe
class EmployeForm(forms.ModelForm):
class Meta:
model = Employe
fields = '__all__'
CodePudding user response:
You should provide some success url inside HttpResponseRedirect as:
return HttpResponseRedirect("/success/")
Or you can call the name of view as:
return HttpResponseRedirect(reverse("some_path_name"))
To redirect on the same page use:
return HttpResponseRedirect(request.path_info)