0

i have a listview of form:

class MatchsView(ListView):
    model = Match2x1
    template_name = 'matchs.html'

and the template render this:

{% for match in object_list %}
    <form action="/apostar/" method="post">{% csrf_token %}
        <p><input type="radio" name="{{match.id}}" value="{{match.team_a}}">{{match.team_a}}</input></p>
        <input type="submit" value="Apostar"></input>
    </form>
{% endfor %}

as you can see each form has two fields, i need to save in DB the values that the user choose, with FormView its easy, but since this time is a ListView im a little bit lose to save in DB from a form, i know that i have to create a view that handles the form, but really i dont know how to create the view that handles the post data of each form. For example lets says that i need to save the post data in a model called FormsMatchs, how can i do it?

i was trying with this:

class FormView(FormView):
    form_class = FormMatch
    success_url = '/'
    template_name = 'matchs.html'

    def post(self, request, *args, **kwargs):
        hola = Country.objects.create(name=request.POST)

but is saving this:

<QueryDict: {u'csrfmiddlewaretoken': [u'tCIQuGlSXKJL0R5eo9R5w09ldeBt7zNW'], u'5': [u'River']}>
dfrojas
  • 673
  • 1
  • 13
  • 32

1 Answers1

0

Your best bet if you want to have a simple list of a given model but also accept a Form is to use a FormView, and override get_context_data(self, **kwargs) to pass a queryset into the context, like so:

def get_context_data(self, **kwargs):
    context = super(MatchsView, self).get_context_data()
    context['object_list'] = Match2x1.objects.all()
    return context

However, you can also use a FormMixin with a ListView, see an example here.

Community
  • 1
  • 1
Joseph
  • 12,678
  • 19
  • 76
  • 115
  • thanks, but is there some way to do it without an override, because the thing is i need only some fields of the form and besides i need to put other field where user fill an info, i forgot to put it but the form has other field and this fields has not come from the model – dfrojas Mar 24 '15 at 23:45