Answer the question
In order to leave comments, you need to log in
Why does a MultiValueDictKeyError occur when voting in the polls plugin for Django CMS?
I'm learning Django CMS, I tried to create a polls plugin for Django CMS 3.2.0 based on the standard django-polls application. Followed instructions in official docks . All code is copied from there. But after placing the plugin on the main page of my project, I see only the question itself and the radio buttons by the number of answers, but I do not see the text of the answers. After selecting one of the answers and pressing the Vote button, I get an error page. Here is the traceback:
Environment:
Request Method: POST
Request URL: localhost:8000/en/polls/2/vote
Django Version: 1.8.7
Python Version: 2.7.10
Installed Applications:
('djangocms_admin_style',
'django.contrib.auth',
'django .contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib .messages',
'debug_toolbar',
'cms',
'menus',
'sekizai',
'treebeard',
'djangocms_text_ckeditor',
'djangocms_style',
'djangocms_column',
'filer',
'easy_thumbnails'
' cmsplugin_filer_image '
' cmsplugin_filer_file '
' cmsplugin_filer_folder '
'
cmsplugin_filer_teaser ' ', 'polls_plugin') Installed Middleware: ('cms.middleware.utils.ApphookReloadMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware .AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms .middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware')
Traceback:
File "C:\Users\Fox\Envs\fatdiet\lib\site-packages\django \core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Fox\Envs\fatdiet\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\Users\Fox\Envs\fatdiet\lib\site-packages\django\views\generic\base.py" in view
71. return self. dispatch(request, *args, **kwargs)
File "C:\Users\Fox\Envs\fatdiet\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, * args, **kwargs)
File "C:\Users\Fox\Envs\fatdiet\lib\site-packages\polls\views.py" in post
26. choice = Choice.objects.get(id=request.POST[' choice_pk'])
File "C:\Users\Fox\Envs\fatdiet\lib\site-packages\django\utils\datastructures.py" in __getitem__
322. raise MultiValueDictKeyError(repr(key))
Exception Type:MultiValueDictKeyError at /ru/polls/2/vote/
Exception Value: "'choice_pk'"
from django.views.generic import DetailView, ListView, RedirectView
from django.core.urlresolvers import reverse_lazy
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from polls.models import Choice, Poll, Vote
class PollListView(ListView):
model = Poll
class PollDetailView(DetailView):
model = Poll
def get_context_data(self, **kwargs):
context = super(PollDetailView, self).get_context_data(**kwargs)
context['poll'].votable = self.object.can_vote(self.request.user)
return context
class PollVoteView(RedirectView):
def post(self, request, *args, **kwargs):
poll = Poll.objects.get(id=kwargs['pk'])
user = request.user
choice = Choice.objects.get(id=request.POST['choice_pk'])
Vote.objects.create(poll=poll, user=user, choice=choice)
messages.success(request, _("Thanks for your vote."))
return super(PollVoteView, self).post(request, *args, **kwargs)
def get_redirect_url(self, **kwargs):
return reverse_lazy('polls:detail', args=[kwargs['pk']])
<h1>{{ instance.poll.question }}</h1>
<form action="{% url 'polls:vote' instance.poll.id %}" method="post" name="form">
{% csrf_token %}
<div class="form-group">
{% for choice in instance.poll.choice_set.all %}
<div class="radio">
<label>
<input type="radio" name="choice" value="{{ choice.id }}">
{{ choice.choice_text }}
</label>
</div>
{% endfor %}
</div>
<input type="submit" value="Vote" />
</form>
Answer the question
In order to leave comments, you need to log in
I found the first step of the solution :)
I had to write name="choice_pk" in the input attributes and output the text as {{choice.choice}} . Apparently, the polls version has been updated and changed.
True, the second error got out, now when voting I get:
IntegrityError at /ru/polls/2/vote/
columns user_id, poll_id are not unique
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question