0

我一直在尝试将 django paginator 实现到我的白板应用程序中,这样我就可以将图片分成不同的页面。

当我尝试在不同页面之间移动时会出现问题。我将每个页面限制为 1 个对象并上传了几张图片以测试页面之间的分页是否有效,但是当我尝试使用分页方法在不同页面之间移动时,它没有响应.

http://img854.imageshack.us/img854/3303/94627386.jpg

我一直在研究和测试通过 django 分页文档解决这个问题的方法,我认为问题出在我的模板中的分页模块方法上。

我的观点.py

 def Boat(request ,animal_id):
         if not request.user.is_authenticated():
             return HttpResponseRedirect(reverse('world:LoginRequest'))

    picture = Picture.objects.filter(board=animal_id)

    paginator = Paginator(picture,1)
    page = request.GET.get('page')
    try:
             picture = paginator.page(page)

         except PageNotAnInteger:
             picture = paginator.page(1)
         picture = paginator.page(paginator.num_pages)


    return render(request,'boat.html',{'picture':picture })

我的船.html

 {% if picture.object_list %}
 <ul>
    {% for pet in picture.object_list %}


        {% if pet.image %}

    <br>
        <img src= "{{ pet.image.url }}" </a>
 <br>
    </a>
 </li>
        {% endif %}

 <br>
 <a href="{% url world:CommentCreator pet.id %}">View Comment</a>  <a href="{% url      world:LikePicture pet.id %}">Like</a><br/>
 {% for c in picture %}
     {% ifequal c.picture.id pet.id %}

 <br>{{ c.body }}</li>
 <br>{{ c.created}}</li>
 <br>{{ c.user}}</li>
     {% endifequal %}
 % endfor %}

 {% endfor %}
 </ul>
 {% endif %}
 <a href="{% url world:PictureCreator %}">Add Pictures to your board</a><br/>

 {% if number %}
 {{number}}
 {% endif %}
 <a href="{% url world:Profile %}">Return back to Profile</a><br/>

 <br><br><br><br><br>

 <div class="pagination">
     <span class="step-links">
         {% if picture.has_previous %}
             <a href="?page={{ picture.previous_page_number }}">previous</a>
         {% endif %}

         <span class="current">
             Page {{ picture.number }} of {{ picture.paginator.num_pages }}.
         </span>

         {% if picture.has_next %}
             <a href="?page={{ picture.next_page_number }}">next</a>
         {% endif %}
     </span>
 </div>

我的模块的一部分

class Picture(models.Model):
    user = models.ForeignKey(User)
    board = models.ForeignKey(Board,blank=False,null=False,related_name='board')
    image = models.FileField(upload_to="images/",blank=True)
    description = models.TextField()
    is_primary = models.BooleanField(default=False)

    def __unicode__(self):
        return self.description
4

2 回答 2

1
def Boat(request ,animal_id):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:LoginRequest'))

    picture = Picture.objects.filter(board=animal_id)

    paginator = Paginator(picture,1)

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

    try:
        picture = paginator.page(page)
    except (EmptyPage, InvalidPage):
        picture = paginator.page(paginator.num_pages)

    picture = paginator.page(paginator.num_pages)

    return render(request,'boat.html',{'picture':picture })
于 2013-03-22T14:50:15.780 回答
0
#this is view 
#this is views files
@login_required(login_url='/login')

# class Base_page_list2(ListView):
def Base_page_list(request,*args, **kwargs):

    # tiket      = Tiket.objects.all()
    
    # lastest_tiket =Tiket.objects.order_by('-id').all()[:8]
    tiket_list = Tiket.objects.all()
    paginator  = Paginator(tiket_list,2)

    page = request.GET.get('page')
    page_obj = paginator.get_page(page)

    context ={
        'tiket':None,
        'page_obj':page_obj,
        

        
    }
    context['tiket']=['page_obj']
    
    
    if request.user.is_superuser:
        context['tiket']= Tiket.objects.all()
             
    elif not request.user.is_hrm:
        raise Http404('شما نمی توانید به این صحفه دسترسی داشته باشید')
    
    elif request.user.is_mis :
        context['tiket']= Tiket.objects.filter(status_tag='s')
    
    elif request.user.is_mali:
        context['tiket']=Tiket.objects.filter(status_tag='m')
    
    elif request.user.is_mosh:
        context['tiket']=Tiket.objects.filter(status_tag='c')

    elif request.user.is_modir:
        context['tiket']=Tiket.objects.filter(status_tag='b')
    
    elif request.user.is_kz:
        context['tiket']=Tiket.objects.filter(status_tag='k')    
    
    elif request.user.is_pa:
        context['tiket']=Tiket.objects.filter(status_tag='p')  
    
    else:
       context['page_obj']['tiket']=Tiket.objects.filter(author=request.user) 
return render(request,'hrm_account/base.html',context)
`enter code here`error

  File "/home/ali/Desktop/testsharen2/sharen/sharen_hrm/views.py", line 128, in Base_page_list
    context['page_obj']['tiket']=Tiket.objects.filter(author=request.user)
TypeError: 'Page' object does not support item assignment
于 2021-10-06T14:08:54.123 回答