Пример #1
0
def create(request):
    
    class RequiredFormSet(BaseFormSet):
        def __init__(self, *args, **kwargs):
            super(RequiredFormSet, self).__init__(*args, **kwargs)
            for form in self.forms:
                form.empty_permitted = False

    ChoiceFormSet = formset_factory(ChoiceForm, max_num=10, formset=RequiredFormSet)
    if request.method == 'POST': # If the form has been submitted...
        poll_form = PollForm(request.POST) # A form bound to the POST data
        # Create a formset from the submitted data
        choice_formset = ChoiceFormSet(request.POST, request.FILES)

        if poll_form.is_valid() and choice_formset.is_valid():
            poll = poll_form.save(commit=False)
            if request.user.is_authenticated():
                poll.creator = request.user
            poll.save()
            poll_form.save_m2m()
            for form in choice_formset.forms:
                choice = form.save(commit=False)
                choice.poll = poll
                choice.save()
            top_polls = Poll.objects.annotate(num_votes=Sum('choice__votes')).order_by('-num_votes')
            top_tags = Poll.tags.most_common()[:10]
            all_tags = Poll.tags.most_common()
            if request.user.is_authenticated():
                auth_form = False
                logged_in = True

            else:
                logged_in = False
                auth_form = AuthenticateForm()
            return render_to_response('polls/detail.html',
                              {"poll": poll, "top_polls": top_polls, "top_tags": top_tags, "auth_form": auth_form,
                               "logged_in": logged_in, "all_tags": all_tags}, context_instance=RequestContext(request))
    else:
        poll_form = PollForm()
        choice_formset = ChoiceFormSet()



    top_polls = Poll.objects.annotate(num_votes=Sum('choice__votes')).order_by('-num_votes')
    top_tags = Poll.tags.most_common()[:10]
    all_tags = Poll.tags.most_common()
    if request.user.is_authenticated():
        auth_form = False
        logged_in = True

    else:
        logged_in = False
        auth_form = AuthenticateForm()
    return render_to_response('polls/create.html',
                              {"top_polls": top_polls, "top_tags": top_tags, "auth_form": auth_form,
                               "logged_in": logged_in, "all_tags": all_tags, "poll_form": poll_form, "choice_formset": choice_formset}, context_instance=RequestContext(request))
Пример #2
0
def create(request):
    class RequiredFormSet(BaseFormSet):
        def __init__(self, *args, **kwargs):
            super(RequiredFormSet, self).__init__(*args, **kwargs)
            for form in self.forms:
                form.empty_permitted = False

    ChoiceFormSet = formset_factory(ChoiceForm,
                                    max_num=10,
                                    formset=RequiredFormSet)
    if request.method == 'POST':  # If the form has been submitted...
        poll_form = PollForm(request.POST)  # A form bound to the POST data
        # Create a formset from the submitted data
        choice_formset = ChoiceFormSet(request.POST, request.FILES)

        if poll_form.is_valid() and choice_formset.is_valid():
            poll = poll_form.save(commit=False)
            if request.user.is_authenticated():
                poll.creator = request.user
            poll.save()
            poll_form.save_m2m()
            for form in choice_formset.forms:
                choice = form.save(commit=False)
                choice.poll = poll
                choice.save()
            top_polls = Poll.objects.annotate(
                num_votes=Sum('choice__votes')).order_by('-num_votes')
            top_tags = Poll.tags.most_common()[:10]
            all_tags = Poll.tags.most_common()
            if request.user.is_authenticated():
                auth_form = False
                logged_in = True

            else:
                logged_in = False
                auth_form = AuthenticateForm()
            return render_to_response('polls/detail.html', {
                "poll": poll,
                "top_polls": top_polls,
                "top_tags": top_tags,
                "auth_form": auth_form,
                "logged_in": logged_in,
                "all_tags": all_tags
            },
                                      context_instance=RequestContext(request))
    else:
        poll_form = PollForm()
        choice_formset = ChoiceFormSet()

    top_polls = Poll.objects.annotate(
        num_votes=Sum('choice__votes')).order_by('-num_votes')
    top_tags = Poll.tags.most_common()[:10]
    all_tags = Poll.tags.most_common()
    if request.user.is_authenticated():
        auth_form = False
        logged_in = True

    else:
        logged_in = False
        auth_form = AuthenticateForm()
    return render_to_response('polls/create.html', {
        "top_polls": top_polls,
        "top_tags": top_tags,
        "auth_form": auth_form,
        "logged_in": logged_in,
        "all_tags": all_tags,
        "poll_form": poll_form,
        "choice_formset": choice_formset
    },
                              context_instance=RequestContext(request))