B
B
Bulbashful2016-05-18 12:08:25
Django
Bulbashful, 2016-05-18 12:08:25

Why does django show a db field every other time?

I am using PostgreSQL.
models.py

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text

index.html
<h3>{{ question.question_text }}</h3>
{% if error_message %} <p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/vote/" method="post">
    <div class="radio">
{% csrf_token %}
    {% for choice in choice_list %}
   <input type="radio"  name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}<br />
<input type="submit" class="btn btn-default" value="Проголосовать" />
        </div>
</form>

views.py
def main(request):
    all_info = Info.objects.all()
    question = Question.objects.filter(pk=1)
    choice = Choice.objects.all()
    return render(request,'main.html',{'all_info':all_info,'question':question,'choice_list': choice})

In index.html, the field <h3>{{ question.question_text }}</h3>is displayed one time after the page is refreshed. May appear 2 or 3 times after the update. What could be the issue here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2016-05-18
@Bulbashful

In the view,
replace this with this
. When you do .filter(..) you get back the list you are trying to display in the template. And when .get(...) is already a specific object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question