tomb
tomb

Reputation: 400

django 'polls' tutorial - moving on from one poll to another

So I've finished the django tutorial but one thing I don't like about it is that you cant click through to the next poll when you answer one. For example if your localhost:8000/polls/ page looked like:

red or white?
black or blue?
Who is your favourite Beatle?

then I'd like to be able to answer the first poll (red or white) then move onto the 'black or blue' question from that button that normally says vote again.

I'm very new to python and django but I'm guessing it just requires a tweak like a "+1" into the {% url 'polls:detail' poll.id %}">Vote again? statement in polls/results.html.

What's the tweak? Thanks,

Upvotes: 1

Views: 410

Answers (2)

meshy
meshy

Reputation: 9116

If your Poll object has a DateField or DateTimeField that is not nullable:

pub_date = models.DateTimeField(...)

Then you can get the next object based upon that datetime with the get_next_by_FOO method that is automatically added by django.

In your template you could then have:

{% with next=object.get_next_by_pub_date %}
    {% if next %}
        <a href="{% url 'polls:detail' next.id %}">next</a>
    {% endif %}
{% endwith %}

Upvotes: 1

sneawo
sneawo

Reputation: 3631

In results view you can add a variable next_question like:

next_question = None
try:
    next_question = Question.objects.filter(id__gt=question_id)[:1][0]
except IndexError:
    pass

And in template:

{% if next_question %}
    <a href="{% url 'polls:detail' next_question.id %}">Next question</a>
{% endif %}

Upvotes: 1

Related Questions