Django - How to Increment Integer Field from User Input?

Django - How to Increment Integer Field from User Input?

I'm new to Django, but I'm building a website where people can make statements regarding certain debatable topics. Right now I've just manually added a few statements for each debate and I'd like the users to have the ability to "vote" on a statement.

Here's the code I have now:

Code from models.py

class Debate(models.Model):
    title = models.CharField(max_length=50)
    description = models.CharField(max_length=100)
    pub_date = models.DateTimeField('Date Published')
    url_slug = models.SlugField(editable=False)
    def __unicode__(self):
        return self.title

class Statement(models.Model):
    statement = models.CharField(max_length=200)
    debate = models.ForeignKey(Debate)
    votes = models.IntegerField(default=0)
    user = models.ForeignKey(User)
    def __unicode__(self):
        return self.statement

Code from views.py

def debate_page(request, url_slug):
    if request.method == 'POST':
        for statement in statements:
            statement.votes = statement.votes+1

    debate = Debate.objects.get(url_slug=url_slug)
    template = get_template('debate_page.html')
    statements = Statement.objects.filter(debate=debate)

    variables = Context ({
        'debate_title': debate.title,
        'debate_description': debate.description,
        'statements': statements,
    })

    output = template.render(variables)
    return HttpResponse(output)

Code from template

{% extends "base.html" %}
{% block title %}{{ debate_title }}{% endblock %}
{% block head %}{{ debate_title }}{% endblock %}
{% block description %}{{ debate_description }}{% endblock %}
{% block content %} 
        {% for statement in statements %}
            <li>{{ statement }} - {{ statement.votes }}
            <input type="checkbox" name="statement" value="{{ statement.id }}" /></li><br />
        {% endfor %}
        <input type="submit" value="Submit" />      
{% endblock %}

So what I'd like is for a visitor to check a box next to the statement that they agree with and then click the submit button. Doing that would increase the votes attribute for the statement by 1. I just can't figure out how to get user input and make a change to the database.

Thanks!

2

2 Answers

kyiphyu's code can be further simplified using F expressions:

from django.db.models import F

Statement.objects.filter(id__in=statements).update(vote=F('vote') + 1)
1

You can get the user input with

statements = request.POST.getlist('statement')

From that list, you can increase the vote count and update database like

for st in statements:
    statement = Statement.objects.get(id=st)
    statement.vote += 1
    statement.save()

Hope this helps you.

6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.