0

I have a form to let users make a new poll, it consists of 2 ModelForms one for the Question model and another for the Choice model

a screenshot:

Make new poll form

i want there to be something like a (+) or (add another choice) button where users can add as many choices as they want and then submit it all.

here's the models.py file

models.py


from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()




class Question(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
    questionText = models.CharField(max_length=150)
    datePublished = models.DateTimeField(auto_now_add=True)

    def str(self):
        return self.questionText


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choiceText = models.CharField(max_length=200)
    choiceVotes = models.IntegerField(default=0)

    def str(self):
        return self.choiceText

forms.py

from django import forms
from .models import Question
from .models import Choice




class NewPollForm(forms.ModelForm):
    class Meta:
        model = Question
        labels = { 
            "questionText":"Poll question"
        }

        fields = ("questionText",)



class NewChoicesForm(forms.ModelForm):
    class Meta:
        model = Choice
        labels = {
            "choiceText":"Choice"
        }
        fields = ("choiceText",)

the view for this form's page you can see it is set for only one choice field right now

from .forms import NewPollForm
from .forms import NewChoicesForm


def newpoll(request):
    if request.user.is_authenticated == True :
        pass
    else:
        return redirect("users:signup")
    if request.method == "POST":
        qform = NewPollForm(request.POST)
        cform = NewChoicesForm(request.POST)
        if qform.is_valid():
            newpoll = qform.save(commit=False)
            newpoll.user = request.user
            newpoll.datePublished = timezone.now()
            newpoll.save()
            cform.instance.question = newpoll
            cform.save()
            return redirect("polls:details", pollid=newpoll.pk)
        else:
            return render (request, "polls/newpoll.html", {"qform":qform, "cform":cform})
    else:
        qform = NewPollForm()
        cform = NewChoicesForm()
        return render (request, "polls/newpoll.html", {"qform":qform, "cform":cform})

  • Does this answer your question? [Dynamically adding a form to a Django formset](https://stackoverflow.com/questions/501719/dynamically-adding-a-form-to-a-django-formset). You will need to use [inline formsets](https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/#inline-formsets). The above linked question shows how to dynamically add more forms to the formset. – Abdul Aziz Barkat Jun 02 '21 at 08:03
  • @AbdulAzizBarkat No, not sure it uses javascript isn't there another way? – mohammed hashim Jun 02 '21 at 08:08
  • And how else would you do things dynamically when you have to do them at the _client side_ without using JavaScript (At least when the client is a browser)? – Abdul Aziz Barkat Jun 02 '21 at 08:22
  • That's what i'm asking for, i'm new this is my first app i was looking for an answer using python in the views or maybe the models.py – mohammed hashim Jun 02 '21 at 08:28

0 Answers0