Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How to increment integer field from user input?

Tags:

django

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!

like image 546
Stephen Roda Avatar asked May 08 '13 02:05

Stephen Roda


People also ask

How do I get integers in Django?

IntegerField in Django Forms is a integer field, for input of Integer numbers. The default widget for this input is NumberInput. It normalizes to a Python Integer. It uses MaxLengthValidator and MinLengthValidator if max_length and min_length are provided.

Which Django model fields is used for integer value?

IntegerField is a integer number represented in Python by a int instance. This field is generally used to store integer numbers in the database. The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.

What is primary key in Django model?

If you'd like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you've explicitly set Field.primary_key , it won't add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).


1 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)
like image 99
Matt Deacalion Avatar answered Oct 02 '22 04:10

Matt Deacalion