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})
def get(self, request, id=None): if id: question = get_object_or_404(Question, id=id) poll_form = PollForm(instance=question) choices = question.choice_set.all() choice_forms = [ChoiceForm(prefix=str(choice.id), instance=choice) for choice in choices] template = 'polls/edit_poll.html' else: poll_form = PollForm(instance=Question()) choice_forms = [ChoiceForm(prefix=str(x), instance=Choice()) for x in range(4)] template = 'polls/new_poll.html' context = {'poll_form': poll_form, 'choice_forms': choice_forms} return render(request, template, context)
def get(self, request, pk, pkc): poll = get_object_or_404(Poll, id=pk) choice = poll.choices.get(id=pkc) form = ChoiceForm(initial={ 'choice': choice.choice, }) return render(request, 'choices/list.html', context={'form': form, 'poll': poll})
def Poll_Add(request): if request.method == 'POST': poll_form = PollForm(request.POST) choice_forms = [ChoiceForm(request.POST, prefix=str(x), instance=Choice()) for x in range(0, 3)] if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]): new_poll = poll_form.save(commit=False) new_poll.created_by = request.user new_poll.save() for cf in choice_forms: new_choice = cf.save(commit=False) new_choice.question = new_poll new_choice.save() return HttpResponseRedirect('/polls/') return render(request, 'polls/new_poll.html',{'poll_form': poll_form, 'choice_forms': choice_forms} ) else: poll_form = PollForm(request.POST) choice_forms = [ChoiceForm(request.POST, prefix=str(x), instance=Choice()) for x in range(0, 3)] return render(request, 'polls/new_poll.html', {'poll_form': poll_form, 'choice_forms': choice_forms})
def get(self,request,id=None): if id: pass else: poll_form = PollForm(instance=Questions()) choice_forms = [ChoiceForm(prefix=str( x), instance=Choice()) for x in range(3)] context = {'poll_form': poll_form, 'choice_forms': choice_forms} return render(request, 'poll/new_poll.html', context)
def Poll_Edit(request, id=None): question = get_object_or_404(Question, id=id) if request.method == 'POST': poll_form = PollForm(request.POST, instance=question) choice_forms = [ChoiceForm(request.POST, prefix=str(choice.id), instance=choice) for choice in question.choice_set.all()] if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]): new_poll = poll_form.save(commit=False) new_poll.created_by = request.user new_poll.save() for cf in choice_forms: new_choice = cf.save(commit=False) new_choice.question = new_poll new_choice.save() return HttpResponseRedirect('/polls/') return render(request,'polls/edit_poll.html',{'poll_form': poll_form, 'choice_forms': choice_forms}) else: poll_form = PollForm(request.POST, instance=question) choice_forms = [ChoiceForm(request.POST, prefix=str(choice.id), instance=choice) for choice in question.choice_set.all()] return render(request, 'polls/edit_poll.html', {'poll_form': poll_form, 'choice_forms': choice_forms})
def get(self, request, id=None): if id: # qus = get_object_or_404(Qus, id) qus = Qus.objects.get(id=id) poll_form = PollForm(instance=qus) choices = qus.choices_set.all() choice_form = [ ChoiceForm(prefix=str(choice.id), instance=choice) for choice in choices ] template = 'poll/edit_poll.html' else: poll_form = PollForm(instance=Qus()) choice_form = [ ChoiceForm(prefix=str(x), instance=Choices()) for x in range(3) ] template = 'poll/new_poll.html' context = {'poll_form': poll_form, 'choice_form': choice_form} return render(request, template, context)
def get(self, request, id=None): context = {} question = get_object_or_404(Questions, id=id) poll_form = PollForm(request.POST, instance=question) choice_forms = [ChoiceForm(request.POST, prefix=str(choice.id), instance=choice) for choice in question.choice_set.all()] # if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]): # new_poll = poll_form.save(commit=False) # new_poll.created_by = request.user # new_poll.save() # for cf in choice_forms: # new_choice = cf.save(commit=False) # new_choice.Question = new_poll # new_choice.save() # return redirect('poll') context = {'poll_form': poll_form, 'choice_forms': choice_forms} return render(request, 'poll/edit_poll.html', context)
def post(self, request, id=None): context = {} if id: return self.put(request, id) poll_form = PollForm(request.POST, instance=Questions()) choice_forms = [ChoiceForm(request.POST, prefix=str( x), instance=Choice()) for x in range(0, 3)] if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]): new_poll = poll_form.save(commit=False) new_poll.created_by = request.user new_poll.save() for cf in choice_forms: new_choice = cf.save(commit=False) new_choice.question = new_poll new_choice.save() return HttpResponseRedirect('/poll/') context = {'poll_form': poll_form, 'choice_forms': choice_forms} return render(request, 'polls/new_poll.html', context)
def put(self, request, id=None): print("put---->") context = {} question = get_object_or_404(Question, id=id) poll_form = PollForm(request.POST, instance=question) choice_forms = [ChoiceForm(request.POST, prefix=str( choice.id), instance=choice) for choice in question.choice_set.all()] if poll_form.is_valid() and all([cf.is_valid() for cf in choice_forms]): new_poll = poll_form.save(commit=False) new_poll.created_by = request.user new_poll.save() for cf in choice_forms: new_choice = cf.save(commit=False) new_choice.question = new_poll new_choice.save() return redirect('polls_list') context = {'poll_form': poll_form, 'choice_forms': choice_forms} return render(request, 'poll/edit_poll.html', context)
def post(self, request): context = {} poll_form = PollForm(request.POST, instance=Qus()) choice_form = [ ChoiceForm(request.POST, prefix=str(x), instance=Choices()) for x in range(3) ] if poll_form.is_valid() and all([cf.is_valid() for cf in choice_form]): new_poll = poll_form.save(commit=False) new_poll.created_by = request.user new_poll.save() for cf in choice_form: new_choice = cf.save(commit=False) new_choice.qus_id = new_poll new_choice.save() return HttpResponseRedirect('/polls/') context = {'poll_form': poll_form, 'choice_form': choice_form} return render(request, 'poll/new_poll.html', context)
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()})
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)
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)