Answer the question
In order to leave comments, you need to log in
Why, after saving the Django contact form when next. at the entrance to the yurl, the Get method works?
The essence of the problem is as follows:
- there is a feedback form (light)
- the form works !!
-BUT, after saving the information in the database from the feedback form to the yurl, where the empty form should open, all calls to the form + an empty form are displayed!!!
QUESTION:
Why is that? How to make the Get method take only an empty feedback form and not all records from the database?
models.py
class Feedback(models.Model):
your_name = models.CharField(max_length=25)
contact_phone = models.CharField(max_length=25)
email = models.EmailField()
def __str__ (self):
return self.your_name
class FeedbackForm(forms.Form):
class Meta:
model = Feedback
fields = ("your_name",
"contact_phone",
"email"
)
def feedback(request):
FeedbackForm = modelformset_factory(Feedback, fields=("your_name", "contact_phone", "email"))
if request.method == "POST":
form = FeedbackForm(request.POST)
if form.is_valid:
form.save()
return render(request, "index.html")
else:
form = FeedbackForm()
return render (request, "feedback.html", {"form":form})
<form action="" method="POST">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit" />
</form>
Answer the question
In order to leave comments, you need to log in
Read carefully in the documentation what the modelformset_factory does ( djbook.ru/rel1.6/topics/forms/formsets.html#formsets) It is clearly not needed here. Just import your form into a view and use it.
Quarrel... Stupid...
Answer - so that my rake is not stepped on:
it was initially loaded from from django import forms
and used class FeedbackForm(forms.Form):
well, but it was necessary:
from django.forms import ModelForm
class FeedbackForm(ModelForm):
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question