0

Alright, here is what I want to do:

A user sees a number of assignments in a table, with each assignment paginated using paginator. A user can input a number in a field and click "go", he is then taken to the page.

Here's my code so far:

View:

def home(request):
if request.user.is_authenticated():
    assignments = show_assignments(request)

    form=GoToForm(request.POST or None)
    if request.POST and form.is_valid():
        form = form.cleaned_data
        assignment = form["assignment"]
        page = form["page"]
        return HttpResponseRedirect("/coding/assignment/%i/?page=%i") % (assignment, page)

    return render(request, 'account.html', {'assignments':assignments, 'form':form})

Template:

{% for assignment in assignments %}
    <tr>
        <td><a href="{% url 'coding:assignment' assignment.id %}">
        {{ assignment.country }}: {{ assignment.start_date }} to {{ assignment.end_date }}</a></td>
        <td>{{ assignment.finished_articles }} of {{ assignment.article_num}} articles finished </td>
        <td><a href="/coding/assignment/{{ assignment.id }}/?page={{ assignment.last_updated}}">Start where I left off</a></td>
        <td>
            <form id="form" action="" method="post" accept-charset="utf-8">
                {% csrf_token %}
                Jump to page {{ form.page }}
                <input type="hidden" name="" value="{{ assignment.id }}" id="id_assignment">

                <input type="submit" value="Go">
            </form>
        </td>
    </tr>
{% endfor %}

This isn't working. Is this because I need to use formsets (because I have multiple GoToForms on one page), or is the HttpResponseRedirect not working?

LukasKawerau
  • 1,071
  • 2
  • 23
  • 42

1 Answers1

0

If the GoToForm form class looks simple (e.g. it only has page field), then there's no need to use this form class at all. Just add form fields manually in template and validate them inside post view if needed.

Formset for this case would be overkill, unless you have a lot of input fields to work with.

Also remember to change id="form" to make it unique for each form or use class="form" instead.

mariodev
  • 13,928
  • 3
  • 49
  • 61