A
A
Andrey Voskresensky2016-03-14 20:54:51
Django
Andrey Voskresensky, 2016-03-14 20:54:51

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

forms.py
class FeedbackForm(forms.Form):
  
  class Meta:
    model = Feedback
    fields = ("your_name", 
      "contact_phone", 
      "email"
    )

views.py
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})

feedback.html
<form action="" method="POST">{% csrf_token %}
    {{form.as_p}}


    
    <input type="submit" value="Submit" />
</form>

this is what it outputs:
0de2b8e4edd347559e4eb22750f1f9bb.PNG

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kazmiruk, 2016-03-14
@kazmiruk

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.

A
Andrey Voskresensky, 2016-03-14
@Voskresenskyi

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 question

Ask a Question

731 491 924 answers to any question