好吧,这就是我想做的事情:
用户在表格中看到许多作业,每个作业都使用分页器进行分页。用户可以在一个字段中输入一个数字并单击“go”,然后他将被带到该页面。
到目前为止,这是我的代码:
看法:
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})
模板:
{% 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 %}
这是行不通的。这是因为我需要使用表单集(因为我在一页上有多个 GoToForms),还是 HttpResponseRedirect 不起作用?