Skip to main content

A plugin for Wagtail that provides polling functionality.

This work is based on wagtailpolls

Installing

Install using pip:

pip install wagtailpolls-ng

It works with Wagtail 2.0.x and upwards.

Using

Add wagtailpolls to your INSTALLED_APPS, add the line from wagtailpolls.views import vote to your urls.py and include the URL url(r'^vote/(?P<poll_pk>.*)/$', vote.vote, name='wagtailpolls_vote').

Define a foreign key referring to wagtailpolls.Poll and use the PollChooserPanel:

from django.db import models
from wagtailpolls.edit_handlers import PollChooserPanel
from wagtail.wagtailadmin.edit_handlers import FieldPanel

class Content(Page):
    body = models.TextField()
    poll = models.ForeignKey(
        'wagtailpolls.Poll',
        null=True,
        blank=True,
        on_delete=models.SET_NULL
    )

    content_panels = [
        FieldPanel('body', classname="full"),
        PollChooserPanel('poll'),
    ]

Then, in your editor, ensure that you have added some polls in the polls section in wagtail admin. You will be able to select a poll from there accessable in the template as you would expect.

Templating & Display

There are many ways in which you may want to display your poll. wagtailpolls comes with a template tag to assist with this, as well as certain attributes accessible via templating to render each question as a form. Here is an example using all of the tools provided:

{% extends "layouts/page.html" %}
{% load wagtailpolls_tags %}
{% block content %}
<h1>{{ self.title }}</h1>
<br>
{% if self.poll %}
<form class='poll' method='POST' action='{% vote self.poll %}'>
{% csrf_token %}
{{self.poll.form}}
<br><br>
    <input type="submit" value="Vote">
</form>
{% else %}
    No polls added to this page yet!
{% endif %}
{% endblock %}

As shown, the {% vote %} template tag will need to be passed a poll instance to function correctly. You will also need to {% load wagtailpolls_tags %} at the top of the file where this template tag is used. The poll can be rendered with all questions using .form at the end. .form_as_ul and all other form types will also work.

If you do select a poll for a page, no fields will display on the form and, upon voting, a message stating that there is no poll to vote on will be displayed.

Voting

When a vote has been submitted, the server will return a JsonResponse something like:

{"total_votes": 11, "total_questions": 3, "poll": "Test Poll", "votes": {"Nah": 10, "Yeah": 1, "Maybe": 0}}

With javascript, this data can be used to create a frontend for your poll to your own liking.

The voting form also performs some validation. If the voting form is unable to obtain your IP it will return something like:

{"poll": "Test Poll", "total_questions": 3, "total_votes": 11, "votes": {"Yeah": 1, "Maybe": 0, "Nah": 10}, "form_error": {"__all__": ["Sorry, we were not able to obtain your ip address"]}}

There is also a WAGTAILPOLLS_VOTE_COOLDOWN which is set in your settings. This will only allow users on the same IP to vote at an interval of your choosing. If this is caught, the error will be present in the JsonResponse much like the error above.

Additionally, information will be added to the django session (basically cookies will be set) that will help make sure devices are not able to vote twice. When a vote is rejected due to this reason, the vote simply won’t register with no error being returned in the JsonResponse.

Settings

The following settings can to be set in your settings.py file.

WAGTAILPOLLS_VOTE_COOLDOWN This is to be an integer representing minutes, the default is 10 minutes.

WAGTAILPOLLS_VOTE_REQUIRE_PERMS A string or list of strings representing the permissions to vote, aka. ‘wagtailadmin.access_admin’

Metadata

Release files for wagtailpolls-ng 0.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wagtailpolls-ng 0.5.3
File Size Uploaded
wagtailpolls-ng-0.5.3.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wagtailpolls-ng 0.5.3
File Interpreter ABI Platform
wagtailpolls_ng-0.5.3-py3-none-any.whl Python 3 none any Details

Total release size: 48.3 kB

Release files / wagtailpolls-ng-0.5.3.tar.gz

Download URL wagtailpolls-ng-0.5.3.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0d3b886bcb87135521e0375b39e5e8b03afb5ddba6db6cef870cbf1257583ff5
BLAKE2b-256 checksum
How to use checksums
49fc9ea525b2cde1d0ce5f0b819d2c4b98209cc054bcde1dcadc9c2b2889eb99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.10

Release files / wagtailpolls_ng-0.5.3-py3-none-any.whl

Download URL wagtailpolls_ng-0.5.3-py3-none-any.whl
Size 29.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a12e6e3260ba0463052c729809f3f9e6e51f7519d933d4c5c94a228f65dff06
BLAKE2b-256 checksum
How to use checksums
35cf2fd3a58c942a4377d2100a33ad4c04cf4f31755b9edb90e9100bc437be79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.10

Release history Release notifications | RSS feed

This release

0.5.3 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page