Click here to Skip to main content
15,306,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I am building this debate app where a question is posted and the user chooses the option to agree,disagree or have a neutral opinion. When the user chooses the option I want them to type the reason in textbox after choosing the option and then submit. After submitting they get to see other users choice. I have build until choosing the options in the vote page and displaying the result but I could not get the textbox. here is my
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
class Poll(models.Model):
    question = models.TextField()
    option_one = models.CharField(max_length=30)
    option_two = models.CharField(max_length=30)
    option_three = models.CharField(max_length=30)
    option_one_count = models.IntegerField(default=0)
    option_two_count = models.IntegerField(default=0)
    option_three_count = models.IntegerField(default=0)
    #point = models.TextField()
    def total(self):
       return self.option_one_count + self.option_two_count + self.option_three_count
class textbox_model(models.Model):
    Poll = models.ForeignKey(Poll,on_delete=models.CASCADE)
    body = models.TextField(max_length = 5000 , null=False,blank=False)
    author = models.ForeignKey(User,on_delete=models.CASCADE)
    def __str__(self):
        return self.body
from django.forms import ModelForm
from .models import Poll,textbox_model
from django import forms

class CreatePollForm(ModelForm):
   class Meta:
       model = Poll
       fields = ['question', 'option_one', 'option_two', 'option_three']
class EditTextbox(forms.ModelForm):
    class Meta:
        model = textbox_model
        fields = ('body',)
        widgets = {
        'body': forms.Textarea(attrs={'class':'form-control'}),
def vote(request, poll_id):
    poll = Poll.objects.get(pk=poll_id)

    if request.method == 'POST':

        selected_option = request.POST['poll']
        if selected_option == 'option1':
            poll.option_one_count += 1
        elif selected_option == 'option2':
            poll.option_two_count += 1
        elif selected_option == 'option3':
            poll.option_three_count += 1
            return HttpResponse(400, 'Invalid form option')

        return redirect('debateapp:results',

    context = {
        'poll' : poll
    return render(request, 'debateapp/vote.html', context)
# to insert textbox in the vote page and show the text in the results page
def textbox(request):
    context ={}
    # add the dictionary during initialization
    form = EditTextbox(request.POST or None)
    if form.is_valid():

    context['form']= form
    return render(request, "debateapp/vote.html", context)
def results(request, poll_id):
    poll = Poll.objects.get(pk=poll_id)
    context = {
        'poll' : poll
    return render(request, 'debateapp/results.html', context)
path('', views.home, name='home'),
    path('results/<poll_id>/', views.results, name='results'),
    path('vote/<poll_id>/',, name='vote'),

What I have tried:

I have tried to create a model named"textbox" and using foreign key I have connected it to the poll. I have created function in named "textbox" and by using forms I tried to display it.
In my vote.html page I have added
. In my results.html page I have added
<li >{{textbox_model.body}}</li>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900