0

In the django basic polls app. I have added the functionality to add polls and I want the "add choice" form to come below the "poll detail" form on the template, how do i do it?

I want the "add choice" to render below the Vote of "detail" template

Add choice view:

def choice_view(request,question_id):
    if request.user.is_staff and request.user.has_perm('poll.add_choice'):
        question=Question.objects.get(pk=question_id)
        if request.method=='POST':
            cform=choice_form(request.POST)
            if cform.is_valid():
                add_poll=cform.save(commit=False)
                add_poll.question=question
                add_poll.vote=0
                add_poll.save()
                cform.save()
            return HttpResponseRedirect(reverse('polls:add_choice',args=(question_id,)))
        else:
            cform=choice_form()
        return render(request,'polls/add_choice.html',{'cform':cform,'question_id':question_id},context_instance=RequestContext(request),)
    else:
        return HttpResponse('Permission Denied')

Add choice template:

{% load crispy_forms_tags %}
{% include 'polls/base2.html' %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add Choices</title>
</head>

<body>
<h1>Add Choices</h1>
<form method="post">
    {% csrf_token %}
    {{ cform.choice_text | as_crispy_field }}
    <button class="btn btn-primary btn-default" type="submit">Add Next</button>
    <a href="{% url 'polls:detail' question_id %}" class="btn btn-warning">Done</a>
</form>

</body>
</html>

Add choice form:

class choice_form(forms.ModelForm):
    class Meta:
        model=Choice
        fields=['choice_text']

Poll detail view:

class DetailView(generic.DetailView):
    model=Question
    template_name = 'polls/detail.html'

Poll detail template:

{% load staticfiles %}
{% include 'polls/base2.html' %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>

     <h1>{{ question.question_text }}</h1>
     {% if error_message %}
         <p><strong>{{ error_message }}</strong></p>
     {% endif %}

     <form action="{% url 'polls:vote' question.id %}" method="post">
         {% csrf_token %}
         {%  for choice in question.choice_set.all %}
            {% if choice.is_voted_by%}
                <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}   " checked="checked"  />
            {% else%}
                <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}  " />
            {% endif%}

            <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br/>
         {% endfor %}
         <br><button class="btn btn-lg btn-primary btn-block" type="submit" style="width: 100px">Vote</button>
     </form>

     <br><a href="{% url 'polls:comment' question.id  %}" class="btn btn-primary btn-default">Leave a comment</a>&nbsp;
     {% if user.is_staff %}
         <a href="{% url 'polls:question_delete' question.id %} " class="btn btn-danger">Delete question</a>
     {% endif %}
     <hr>
        <h3>Comments</h3>

        {% for comment in question.comments.all %}
            <br>{{ comment.body }}
            {% if user.is_staff %}
                <a href="{% url 'polls:comment_delete' question.id comment.id %} ">Delete comment</a>
            {% endif %}
            <br>--{{ comment.email }}<br>
        {% endfor %}
</body>
</html>

0 Answers0