コード例 #1
0
 def post(self, request, pk, pkc):
     poll = get_object_or_404(Poll, id=pk)
     choice = poll.choices.get(id=pkc)
     form = ChoiceForm(data=request.POST)
     if form.is_valid():
         choice.choice = form.cleaned_data.get('choice')
         choice.poll = poll
         choice.save()
         return redirect('choice-list', pk=poll.id)
     return render(request, 'choices/list.html', context={'form': form})
コード例 #2
0
ファイル: views.py プロジェクト: anishchapagain/djangoPoll
def add_choice(request, question_id):
    question = Question.objects.get(id=question_id)
    if request.method == 'POST':
        form = ChoiceForm(request.POST)
        if form.is_valid():  # form.cleaned_data
            # print("FORM",form)
            # addOption = form.save()
            # print("\tOPTION",addOption)
            # addOption.question = question
            # addOption.votes = votes
            # addOption.save()
            form.save()
            return HttpResponseRedirect(reverse('poll:results', args=(question.id,)))
            # return render(request, 'poll/detail.html', {'questions': questions})
        else:
            print(form.errors)
    else:
        form = ChoiceForm()
    return render(request, 'poll/add_choice.html', {'form': form, 'question': question, 'count': calculate()})
コード例 #3
0
ファイル: views.py プロジェクト: themaleem/Django-polls
def edit_choice(request, choice_id):
    choice = get_object_or_404(Choice, id=choice_id)
    if request.user != choice.poll.owner:
        return HttpResponse(
            "You're not authorized to make changes to this choice")

    if request.method == "POST":
        form = ChoiceForm(request.POST, instance=choice)
        if form.is_valid():

            form.save()
            messages.success(
                request,
                "Choice edited successfully",
                extra_tags='alert alert-success alert-dismissible fade show')
            return redirect(
                reverse('poll:edit_poll', kwargs={'poll_id': choice.poll.id}))
    else:
        form = ChoiceForm(instance=choice)

    context = {'form': form, 'choice': choice}
    return render(request, 'poll/edit_choice.html', context)
コード例 #4
0
ファイル: views.py プロジェクト: themaleem/Django-polls
def add_choice(request, poll_id):
    poll = get_object_or_404(Poll, id=poll_id)
    if request.user != poll.owner:
        return HttpResponse("You're not authorized to view this page")

    if request.method == "POST":
        form = ChoiceForm(request.POST)
        if form.is_valid():
            new_choice = form.save(commit=False)
            new_choice.poll = poll
            new_choice.save()
            messages.success(
                request,
                "Choice added successfully",
                extra_tags='alert alert-success alert-dismissible fade show')
            return redirect(
                reverse('poll:details', kwargs={'poll_id': poll_id}))
    else:
        form = ChoiceForm()

    context = {'poll': poll, 'form': form}
    return render(request, 'poll/add_choice.html', context)