没有主键的Django分页

2024-05-19 01:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在views.py中有此功能,它将在研究管理系统中显示以下几个教程中的所有类别:

def CategoryView(request):
    cat_menu=Category.objects.all();
    return render(request, 'Category.html',{'cat_menu':cat_menu})

当用户点击某个类别时,他们将获得该类别下的所有研究论文

视图.py

def ResearchCategoryView(request,cats):


    category_research=ResearchesList.objects.filter(research_category=cats.replace('_',' '))
    paginator=Paginator(category_research,1)
    page_number = request.GET.get('page',1)
    page_obj = paginator.get_page(page_number)
    return render(request,'Researches-Guest.html',{'cats':cats.title().replace('_',' '),'category_research':category_research,'page_obj':page_obj,})

我想在Research CategoryView中添加一个paginator,只是我无法链接到与

类别不是研究表中的主键。 有没有一种不用重写ResearchCategoryView就能做到这一点的方法

url.py

path('',views.GuestHome,name="GuestHome"),
path('FeedBackForm',views.Form2,name="FeedBackForm"),
path('FeedBackRecieved',views.ThankYou,name="ThankYou"),
path('Category/',CategoryView,name='Categorys'),
path('Category/<str:cats>',ResearchCategoryView,name='researchcategory'),
path('Category/cats',ResearchCategoryView,name='researchcategory2'),
path('Research/<int:pk>',AbstractView.as_view(),name='abstract'),
path('search_research', views.search_research, name='search_research'),

**已从mySQL检查models.py**

class ResearchesList(models.Model):
    research_id = models.AutoField(primary_key=True)
    title = models.TextField()
    auther = models.TextField()
    auther1 = models.TextField(blank=True, null=True)
    auther2 = models.TextField(blank=True, null=True)
    auther3 = models.TextField(blank=True, null=True)
    auther4 = models.TextField(blank=True, null=True)
    auther5 = models.TextField(blank=True, null=True)
    auther6 = models.TextField(blank=True, null=True)
    abstract = models.TextField()
    publish_date = models.DateField()
    start_date = models.DateField()
    finish_date = models.DateField()
    research_url = models.TextField()
    journal_name = models.CharField(max_length=100)
    research_category = models.CharField(max_length=70)
    data_type = models.CharField(max_length=45, blank=True, null=True)
    data_source = models.TextField(blank=True, null=True)
    geo_area = models.CharField(max_length=45, blank=True, null=True)
    is_collected_via_socialmedia = models.CharField(max_length=45, blank=True, null=True)
    collecation_start_date = models.DateField(blank=True, null=True)
    finishing_colleaction_date = models.DateField(blank=True, null=True)
    date_of_the_data = models.DateField(blank=True, null=True)
    sample_size = models.IntegerField(blank=True, null=True)
    sampling_method = models.TextField(blank=True, null=True)
    data_collecation = models.TextField(blank=True, null=True)
    data_analysis = models.TextField(blank=True, null=True)
    write_up = models.TextField(blank=True, null=True)
    publication = models.TextField(blank=True, null=True)
    capcity_of_building = models.TextField(blank=True, null=True)
    varisble_and_data_dic_name = models.CharField(max_length=100, blank=True, null=True)
    actual_data_name = models.TextField(blank=True, null=True)
    name_of_the_file = models.TextField(blank=True, null=True)
    file_format = models.CharField(max_length=45, blank=True, null=True)
    file = models.CharField(max_length=100, blank=True, null=True)
    lesson_learnt = models.TextField(blank=True, null=True)
    other_comment = models.TextField(blank=True, null=True)
    is_this_data_public = models.CharField(max_length=45)
    status = models.CharField(max_length=45)
    user_id = models.IntegerField(db_column='user-id', blank=True, null=True)  # Field renamed to remove unsuitable characters.

    class Meta:
        managed = False
        db_table = 'researches_list'



class Category(models.Model):
    category_name = models.CharField(primary_key=True, max_length=70)

    class Meta:
        managed = False
        db_table = 'category'



研究Guest.html

<table class="table table-hover p-table">

                      {% if category_research%}

                          {%for item in category_research%}

                      {% if item.status == "Approved" %}

                          <tbody>
                          <tr>
                              <td class="p-name">
                                  <a href="{% url 'abstract' item.pk%}">{{item.title}}</a> by {{item.auther1}},  {{item.auther2}},  {{item.auther3}}, {{item.auther4}},{{item.auther5}},{{item.auther6}}
                                  <br>
                                  <small> Published by:{{item.auther}}  Publising date:{{item.publish_date}}</small>
                              </td>
                          </tr>


                          </tbody>

                      {% endif %}

                       {% endfor%}
                      {%else%}
                        <h2>This Category doesn't exist</h2>
                      {%endif%}
                  </table>
{% if page_obj.has_previous%}
                  <span class=pull-left">

                  <a href="#"> <button type="submit" class="btn btn-info">Previous</button></a>

                    </span>
                   {% endif %}
                  {% if page_obj.has_next%}
                   <span class="pull-right">

                  <a href="#"> <button type="submit" class="btn btn-info">Next</button></a>

                   </span>
                        {% endif %}

Category.html

{%for item in cat_menu%}
                <table class="table">

                              <tbody>
                              <tr>
                                  <td>
                                        <a href="{% url 'researchcategory' item.category_name%}">{{item.category_name}}</a>
                                  </td>

                              </tr>

                              </tbody>
                          </table>
              {% endfor %}

Tags: nametruemodelspagetableitemnulllength

热门问题