0

我有一个页面显示学生选择的类别的所有对象。我在网站上实现了一个分页来分割不同页面的对象。

当学生从保管箱中选择一个类别并尝试浏览新旧条目的分页时,就会出现问题。

在此处输入图像描述

之所以会发生这种情况,是因为每次用户从 dropbox 中选择一个类别时,dropbox 在检索到对象后都会被重置。因此,当用户尝试使用分页浏览对象时。分页不知道要检索哪些数据,因为保管箱类别被重置并将用户重定向到空白页面。

对此的解决方案是对 Dropbox 进行编程,使其针对用户做出的选择保持静态,这样当用户浏览由分页拆分的数据时,分页知道可以根据 Dropbox 检索对象。

我不知道如何让这个 Dropbox 对用户做出的选择保持静态。

我的意见.py

def BoardFinder(request):
    form = BoardFinderForm(request.POST)
    fo = BoardFinderForm()
    if form.is_valid():
        Category = form.cleaned_data['Category']
        posts = Board.objects.filter(Category=Category)
        paginator = Paginator(posts, 1)

        try: page = int(request.GET.get("page", '1'))
        except ValueError: page = 1

        try:
            posts = paginator.page(page)
        except (InvalidPage, EmptyPage):
            posts = paginator.page(paginator.num_pages)
        return render(request,"boardfinder.html",{"posts":posts,"fo":fo})
    return render(request,"boardfinder.html",{"fo":fo})

我的模型.py

class Board(models.Model):
    MATH = 'MATH'
    ENGLISH = 'ENGLISH'
    SCIENCE = 'SCIENCE'
    LANGUAGE = 'LANGUAGE'
    CATEGORY = (
        (MATH, 'Math'),
        (ENGLISH, 'English'),
        (SCIENCE, 'Science'),
        (LANGUAGE, 'Language'),
    )
    Category =models.CharField(max_length=30,choices=CATEGORY)
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    picture = models.OneToOneField('Picture',related_name='picture',blank=True,null=True)
    def __unicode__(self):
        return self.name

class BoardFinderForm(forms.ModelForm):
    class Meta:
        model = Board
        fields = ('Category',)

我的 boardfinder.html 的重要部分

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ fo.as_p }}
<input type = "submit" value= "Find WhiteBoard" />
</form>



            {% for post in posts.object_list %}
                <div class="title">{{ post.name }}</div>
                {% endfor %}
4

1 回答 1

0
<form method="GET">
    <p><select name="category">
        <option value="" 
            {% if not request.session.category %}selected{% endif %}>
            (All subjects)
        </option>

        <option value="ENGLISH" 
            {% if request.session.category == "ENGLISH" %}selected{% endif %}>
            English
        </option>

        <option value="LANGUAGE"
            {% if request.session.category == "LANGUAGE" %}selected{% endif %}>
            Language
        </option>

        <option value="MATH"
            {% if request.session.category == "MATH" %}selected{% endif %}>
            Math
        </option>

        <option value="SCIENCE"
            {% if request.session.category == "SCIENCE" %}selected{% endif %}>
            Science
        </option>
    </select></p>

    <input type = "submit" value= "Find WhiteBoard" />
</form>

def BoardFinder(request):
    category = request.GET.get('category')
    if category:
        request.session['category'] = category
        posts = Board.objects.filter(Category=category)
        paginator = Paginator(posts, 1)

        try: page = int(request.GET.get("page", '1'))
        except ValueError: page = 1

        try:
            posts = paginator.page(page)
        except (InvalidPage, EmptyPage):
            posts = paginator.page(paginator.num_pages)
        return render(request,"boardfinder.html",{"posts":posts,"fo":fo})
    return render(request,"boardfinder.html",{"fo":fo})
于 2013-03-23T12:34:35.233 回答