[Fixed]-How to implement the official django polls example equivalently by using forms?

0πŸ‘

βœ…

{##########################################}
{## pollsForm/templates/pollsForm/detail.html#}
{##########################################}
{###pay attention to {{choice_forms}}###}

<h1> {{ question.question_text }}</h1>

<form action="{% url 'pollsForm:vote' question.id %}" method="post"> {% csrf_token %}
    {{ choice_forms }}
    <input type="submit" value="Vote" />
</form>

{##########################################}
{## pollsForm/templates/pollsForm/results.html#}
{##########################################}
{#This is optional page. This is from official tutorial##}
<h1>{{ question.question_text }}</h1>

<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>

<a href="{% url 'pollsForm:vote' question.id %}">Vote again?</a>   




########################################
# pollsForm/models.py
########################################

same

########################################
# pollsForm/urls.py
########################################

from django.conf.urls import url

from . import views

# namespace='pollsForm'
# append the following line to the myproject/urls.py
#     url(r'^pollsForm/', include('pollsForm.urls', namespace='pollsForm')),

urlpatterns = [
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
]

########################################
# pollsForm/views.py
########################################

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from django.views.decorators.csrf import csrf_protect
from .models import Choice, Question
from django import forms
from django.forms import widgets
from .forms import ChoiceForm

@csrf_protect
def vote(request, question_id):
    p = get_object_or_404(Question, pk=question_id)
    c_set = Choice.objects.filter(question=question_id)

    if request.method == 'GET':
        cforms = ChoiceForm()
        cforms.fields['choice_text'] = forms.ModelChoiceField(queryset=c_set,
                                                              empty_label=None,
                                                              widget=widgets.RadioSelect)
        variables = {
            'choice_forms': cforms,
            'question': p,
        }
        return render(
            request,
            'pollsForm/detail.html',
            variables,
        )

    if request.method == 'POST':
        form = ChoiceForm(request.POST)
        if form.is_valid():
            pk = form.cleaned_data['choice_text']
            selected_choice = p.choice_set.get(pk=pk)
            selected_choice.votes += 1
            selected_choice.save()
            return HttpResponseRedirect(reverse('pollsForm:results', args=(p.id,)))

        if not form.is_valid():
            # change input char to radio
            form.fields['choice_text'] = forms.ModelChoiceField(queryset=c_set,
                                                                empty_label=None,
                                                                widget=widgets.RadioSelect)
            variables = {
               'choice_forms' : form,
                'question': p,
            }
            return render(
                request,
                'pollsForm/detail.html',
                variables,
            )

# optional
class ResultsView(generic.DetailView):
    model = Question
    template_name = 'pollsForm/results.html'

This is example how to handle radio forms as backend.

This is pollsForm app equivalently to official polls app, but using forms instead of messing around front-end. It simply generates {{ choice_forms }} to the front-end.

πŸ‘€anonymous

1πŸ‘

First of all you have to define a form. Convention is to do this in forms.py. A form is a Class that inherits from β€œforms”. You can either have forms.Form, where you have to define your Form yourself, or forms.ModelForm, which will generate a form based on a model.

I will provide an example for the Question model to get you started, but you should read this for more input: https://docs.djangoproject.com/en/1.10/topics/forms/

Oh, and if you are looking for β€œbehind the scenes magic” you can look into Class-based view. They handle a lot for you. They are a bit complicated to customize, but there are a few youtube turorials on the topic.

Example for forms.ModelForm:

class QuestionForm(forms.ModelForm):

class Meta:
    # Define the model here: 
    model = Question
    # Define the fields that you want to show up. Alternatively you can use "exclude", in which you specify the fields that you dont want to show up.
    fields = [
            "question_text",
            "pub_date",
    ]
    # You can use labels to define custom labels.
    labels = {
            "question_text": "Question Headline",
            }

    # Specify the widgets you want to use, if they differ from the standard widgets defined by the modelField. This is especialy usefull for ForeignKeys.
    #widgets = {
    #    'question': forms.HiddenInput,
    #}
πŸ‘€Harper

Leave a comment