0

I am trying to create an object in my views.py that is an integer from models.py. Usually we have lists in the models.py objects, but in this case it is just a single number.

def answer(request, level_id):
    o = Level.objects.get(id=level_id)
    correct = o.answers.filter(value__iexact=guess).exists() <--- working boolean function
    points = o.points.get('points')
    values = { 'score':points }
    return render_to_response('game.html', values)

This gives us an error:

Int has no attribute get.

We are trying to create the points variable to contain the number of points from our models.py:

class Level(models.Model):
    points = models.IntegerField("Point Value",default=1)

1 Answer 1

4

Once you have your Level object, you can just call o.points to get the value of the points attribute, you don't need to call o.points.get('points').

As o.points is an integer, you're seeing this error because integers do not have a .get method.

Sign up to request clarification or add additional context in comments.

3 Comments

Our error is gone. That is great. But our template engine won't print {{score}}
Yes it does. We added a value in admin. Plus gave it a default value of 1, as is written in the code above. I actually was able to display the o.points after putting inside another object in my Level view. But I can't get the same variable to display in my Game view.
That sounds like a different question, which would require different background to answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.