Python Can';无法使用相关fk集获取django分页工作

Python Can';无法使用相关fk集获取django分页工作,python,django,django-views,pagination,bulma,Python,Django,Django Views,Pagination,Bulma,我是一个django初学者,我正在尝试建立一个只使用通用CBV的网站 在我的模型中,我有一个名为Project的类和另一个名为ProjectFile的类,如下所示: class Project(models.Model): STATUS = ( ('Ongoing', 'Ongoing'), ('Ended', 'Ended'), ('Abandoned', 'Abandoned'), ) name = models.Cha

我是一个django初学者,我正在尝试建立一个只使用通用CBV的网站

在我的模型中,我有一个名为Project的类和另一个名为ProjectFile的类,如下所示:

class Project(models.Model):
    STATUS = (
        ('Ongoing', 'Ongoing'),
        ('Ended', 'Ended'),
        ('Abandoned', 'Abandoned'),
    )
    name = models.CharField(max_length=200, unique=True, error_messages={'unique':"This project name already exists"}, null=True)
    description = models.TextField(null=True)
    status = models.CharField(max_length=100, null=True, choices=STATUS)
    start_date = models.DateField(null=True)
    end_date = models.DateField(blank=True, null=True)
    team = models.ForeignKey(Team, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class ProjectFile(models.Model):
    file = models.FileField(upload_to='project_files/')
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
class ProjectList(ListView):
    model = Project
    context_object_name = 'projects'
    paginate_by = 5

    def get_queryset(self):
        return Project.objects.all().order_by('name')

    def get_context_data(self, **kwargs):
        ctx = super(ProjectList, self).get_context_data(**kwargs)
        ctx['projects_count'] = self.get_queryset().count()
        ctx['ongoing_count'] = Project.objects.filter(status='Ongoing').count()
        ctx['ended_count'] = Project.objects.filter(status='Ended').count()
        return ctx
我创建了一个CBV视图,以列出所有名为ProjectList的项目,如下所示:

class Project(models.Model):
    STATUS = (
        ('Ongoing', 'Ongoing'),
        ('Ended', 'Ended'),
        ('Abandoned', 'Abandoned'),
    )
    name = models.CharField(max_length=200, unique=True, error_messages={'unique':"This project name already exists"}, null=True)
    description = models.TextField(null=True)
    status = models.CharField(max_length=100, null=True, choices=STATUS)
    start_date = models.DateField(null=True)
    end_date = models.DateField(blank=True, null=True)
    team = models.ForeignKey(Team, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class ProjectFile(models.Model):
    file = models.FileField(upload_to='project_files/')
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
class ProjectList(ListView):
    model = Project
    context_object_name = 'projects'
    paginate_by = 5

    def get_queryset(self):
        return Project.objects.all().order_by('name')

    def get_context_data(self, **kwargs):
        ctx = super(ProjectList, self).get_context_data(**kwargs)
        ctx['projects_count'] = self.get_queryset().count()
        ctx['ongoing_count'] = Project.objects.filter(status='Ongoing').count()
        ctx['ended_count'] = Project.objects.filter(status='Ended').count()
        return ctx
我在模板中添加了分页,如下所示:

{% if is_paginated %}
                <nav class="pagination is-centered is-rounded">
                    {% if page_obj.has_previous %}
                        <a class="pagination-previous" href="?page=
        {{ page_obj.previous_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}"><i class="fa fa-hand-o-left"></i>&nbsp;{% trans "Previous" %}</a>
                    {% else %}
                        <a class="pagination-previous" disabled>{% trans "Previous" %}</a>
                    {% endif %}
                    {% if page_obj.has_next %}
                        <a class="pagination-next"
                           href="?page={{ page_obj.next_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{% trans "Next" %}&nbsp;<i class="fa fa-hand-o-right"></i></a>
                    {% else %}
                        <a class="pagination-next" disabled>{% trans "Next" %}</a>
                    {% endif %}
                    <ul class="pagination-list">
                        {% for page in paginator.page_range %}
                            <li><a class="pagination-link{% ifequal page page_obj.number %} is-current{% endifequal %}"
                                   href="?page={{ page|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{{ page|stringformat:"d" }}</a></li>
                        {% endfor %}
                    </ul>
                </nav>
class ProjectFileList(ListView):
    model = ProjectFile
    context_object_name = 'file_list'
    template_name = 'projects/project_detail.html'
    paginate_by = 2
    pk_url_kwarg = 'file_pk'
{% for file in project.projectfile_set.all %}
                    <tr>
                        <th><p class="has-text-centered"><a href="{{ file.file.url }}" download target="_blank" >{{ file.filename }}</a></th>
                        <td><p class="has-text-centered">{{ file.file.size|sizify }}</p></td>
                        <td><p class="has-text-centered">{{ file.date_created }}</p></td>
                        <td><a href="#"><p class="has-text-centered"><span class="icon is-small has-text-success"> <i class="fa fa-download"></i></span></p></a></td>
                        <td><a href="{% url 'file-update' project_pk=project.pk file_pk=file.pk %}"><p class="has-text-centered"><span class="icon is-small has-text-info"> <i class="fa fa-edit"></i></span></p></a></td>
                        <td><a href="{% url 'file-delete' project_pk=project.pk file_pk=file.pk %}"><p class="has-text-centered"><span class="icon is-small has-text-danger"> <i class="fa fa-trash"></i></span></p></a></td>
                    </tr>
                {% endfor %}

                </tbody>
            </table>
            {% if is_paginated %}
                <nav class="pagination is-centered is-rounded">
                    {% if page_obj.has_previous %}
                        <a class="pagination-previous" href="?page=
        {{ page_obj.previous_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}"><i class="fa fa-hand-o-left"></i>&nbsp;{% trans "Previous" %}</a>
                    {% else %}
                        <a class="pagination-previous" disabled>{% trans "Previous" %}</a>
                    {% endif %}
                    {% if page_obj.has_next %}
                        <a class="pagination-next"
                           href="?page={{ page_obj.next_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{% trans "Next" %}&nbsp;<i class="fa fa-hand-o-right"></i></a>
                    {% else %}
                        <a class="pagination-next" disabled>{% trans "Next" %}</a>
                    {% endif %}
                    <ul class="pagination-list">
                        {% for page in paginator.page_range %}
                            <li><a class="pagination-link{% ifequal page page_obj.number %} is-current{% endifequal %}"
                                   href="?page={{ page|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{{ page|stringformat:"d" }}</a></li>
                        {% endfor %}
                    </ul>
                </nav>
urlpatterns = [
    path('', ProjectList.as_view(), name='projects-list'),
    path('create/', ProjectCreate.as_view(), name='project-create'),
    path('<int:pk>/', ProjectDetail.as_view(), name='project-detail'),
    path('<int:pk>/update/', ProjectUpdate.as_view(), name='project-update'),
    path('<int:pk>/delete/', ProjectDelete.as_view(), name='project-delete'),

    path('<int:project_pk>/files/add/', ProjectFileCreate.as_view(), name='file-create'),
    path('<int:project_pk>/files/<int:file_pk>/update/', ProjectFileUpdate.as_view(), name='file-update'),
    path('<int:project_pk>/files/<int:file_pk>/delete/', ProjectFileDelete.as_view(), name='file-delete'),
]
在这样的模板中:

{% if is_paginated %}
                <nav class="pagination is-centered is-rounded">
                    {% if page_obj.has_previous %}
                        <a class="pagination-previous" href="?page=
        {{ page_obj.previous_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}"><i class="fa fa-hand-o-left"></i>&nbsp;{% trans "Previous" %}</a>
                    {% else %}
                        <a class="pagination-previous" disabled>{% trans "Previous" %}</a>
                    {% endif %}
                    {% if page_obj.has_next %}
                        <a class="pagination-next"
                           href="?page={{ page_obj.next_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{% trans "Next" %}&nbsp;<i class="fa fa-hand-o-right"></i></a>
                    {% else %}
                        <a class="pagination-next" disabled>{% trans "Next" %}</a>
                    {% endif %}
                    <ul class="pagination-list">
                        {% for page in paginator.page_range %}
                            <li><a class="pagination-link{% ifequal page page_obj.number %} is-current{% endifequal %}"
                                   href="?page={{ page|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{{ page|stringformat:"d" }}</a></li>
                        {% endfor %}
                    </ul>
                </nav>
class ProjectFileList(ListView):
    model = ProjectFile
    context_object_name = 'file_list'
    template_name = 'projects/project_detail.html'
    paginate_by = 2
    pk_url_kwarg = 'file_pk'
{% for file in project.projectfile_set.all %}
                    <tr>
                        <th><p class="has-text-centered"><a href="{{ file.file.url }}" download target="_blank" >{{ file.filename }}</a></th>
                        <td><p class="has-text-centered">{{ file.file.size|sizify }}</p></td>
                        <td><p class="has-text-centered">{{ file.date_created }}</p></td>
                        <td><a href="#"><p class="has-text-centered"><span class="icon is-small has-text-success"> <i class="fa fa-download"></i></span></p></a></td>
                        <td><a href="{% url 'file-update' project_pk=project.pk file_pk=file.pk %}"><p class="has-text-centered"><span class="icon is-small has-text-info"> <i class="fa fa-edit"></i></span></p></a></td>
                        <td><a href="{% url 'file-delete' project_pk=project.pk file_pk=file.pk %}"><p class="has-text-centered"><span class="icon is-small has-text-danger"> <i class="fa fa-trash"></i></span></p></a></td>
                    </tr>
                {% endfor %}

                </tbody>
            </table>
            {% if is_paginated %}
                <nav class="pagination is-centered is-rounded">
                    {% if page_obj.has_previous %}
                        <a class="pagination-previous" href="?page=
        {{ page_obj.previous_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}"><i class="fa fa-hand-o-left"></i>&nbsp;{% trans "Previous" %}</a>
                    {% else %}
                        <a class="pagination-previous" disabled>{% trans "Previous" %}</a>
                    {% endif %}
                    {% if page_obj.has_next %}
                        <a class="pagination-next"
                           href="?page={{ page_obj.next_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{% trans "Next" %}&nbsp;<i class="fa fa-hand-o-right"></i></a>
                    {% else %}
                        <a class="pagination-next" disabled>{% trans "Next" %}</a>
                    {% endif %}
                    <ul class="pagination-list">
                        {% for page in paginator.page_range %}
                            <li><a class="pagination-link{% ifequal page page_obj.number %} is-current{% endifequal %}"
                                   href="?page={{ page|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{{ page|stringformat:"d" }}</a></li>
                        {% endfor %}
                    </ul>
                </nav>
urlpatterns = [
    path('', ProjectList.as_view(), name='projects-list'),
    path('create/', ProjectCreate.as_view(), name='project-create'),
    path('<int:pk>/', ProjectDetail.as_view(), name='project-detail'),
    path('<int:pk>/update/', ProjectUpdate.as_view(), name='project-update'),
    path('<int:pk>/delete/', ProjectDelete.as_view(), name='project-delete'),

    path('<int:project_pk>/files/add/', ProjectFileCreate.as_view(), name='file-create'),
    path('<int:project_pk>/files/<int:file_pk>/update/', ProjectFileUpdate.as_view(), name='file-update'),
    path('<int:project_pk>/files/<int:file_pk>/delete/', ProjectFileDelete.as_view(), name='file-delete'),
]
{%for project.projectfile\u set.all%}

{{file.file.size | sizify}

{{file.date\u created}

{%endfor%} {%if已分页%} {%如果页面_obj.has_previous%} {%else%} {%else%} {%endfor%}
我正在获取与我选择的项目相关的所有文件,但分页不起作用…我知道我必须从我的视图中查询这些文件,但我努力了,但没有成功

我的URL如下所示:

{% if is_paginated %}
                <nav class="pagination is-centered is-rounded">
                    {% if page_obj.has_previous %}
                        <a class="pagination-previous" href="?page=
        {{ page_obj.previous_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}"><i class="fa fa-hand-o-left"></i>&nbsp;{% trans "Previous" %}</a>
                    {% else %}
                        <a class="pagination-previous" disabled>{% trans "Previous" %}</a>
                    {% endif %}
                    {% if page_obj.has_next %}
                        <a class="pagination-next"
                           href="?page={{ page_obj.next_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{% trans "Next" %}&nbsp;<i class="fa fa-hand-o-right"></i></a>
                    {% else %}
                        <a class="pagination-next" disabled>{% trans "Next" %}</a>
                    {% endif %}
                    <ul class="pagination-list">
                        {% for page in paginator.page_range %}
                            <li><a class="pagination-link{% ifequal page page_obj.number %} is-current{% endifequal %}"
                                   href="?page={{ page|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{{ page|stringformat:"d" }}</a></li>
                        {% endfor %}
                    </ul>
                </nav>
class ProjectFileList(ListView):
    model = ProjectFile
    context_object_name = 'file_list'
    template_name = 'projects/project_detail.html'
    paginate_by = 2
    pk_url_kwarg = 'file_pk'
{% for file in project.projectfile_set.all %}
                    <tr>
                        <th><p class="has-text-centered"><a href="{{ file.file.url }}" download target="_blank" >{{ file.filename }}</a></th>
                        <td><p class="has-text-centered">{{ file.file.size|sizify }}</p></td>
                        <td><p class="has-text-centered">{{ file.date_created }}</p></td>
                        <td><a href="#"><p class="has-text-centered"><span class="icon is-small has-text-success"> <i class="fa fa-download"></i></span></p></a></td>
                        <td><a href="{% url 'file-update' project_pk=project.pk file_pk=file.pk %}"><p class="has-text-centered"><span class="icon is-small has-text-info"> <i class="fa fa-edit"></i></span></p></a></td>
                        <td><a href="{% url 'file-delete' project_pk=project.pk file_pk=file.pk %}"><p class="has-text-centered"><span class="icon is-small has-text-danger"> <i class="fa fa-trash"></i></span></p></a></td>
                    </tr>
                {% endfor %}

                </tbody>
            </table>
            {% if is_paginated %}
                <nav class="pagination is-centered is-rounded">
                    {% if page_obj.has_previous %}
                        <a class="pagination-previous" href="?page=
        {{ page_obj.previous_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}"><i class="fa fa-hand-o-left"></i>&nbsp;{% trans "Previous" %}</a>
                    {% else %}
                        <a class="pagination-previous" disabled>{% trans "Previous" %}</a>
                    {% endif %}
                    {% if page_obj.has_next %}
                        <a class="pagination-next"
                           href="?page={{ page_obj.next_page_number|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{% trans "Next" %}&nbsp;<i class="fa fa-hand-o-right"></i></a>
                    {% else %}
                        <a class="pagination-next" disabled>{% trans "Next" %}</a>
                    {% endif %}
                    <ul class="pagination-list">
                        {% for page in paginator.page_range %}
                            <li><a class="pagination-link{% ifequal page page_obj.number %} is-current{% endifequal %}"
                                   href="?page={{ page|stringformat:"d" }}{{ getvars }}{{ hashtag }}">{{ page|stringformat:"d" }}</a></li>
                        {% endfor %}
                    </ul>
                </nav>
urlpatterns = [
    path('', ProjectList.as_view(), name='projects-list'),
    path('create/', ProjectCreate.as_view(), name='project-create'),
    path('<int:pk>/', ProjectDetail.as_view(), name='project-detail'),
    path('<int:pk>/update/', ProjectUpdate.as_view(), name='project-update'),
    path('<int:pk>/delete/', ProjectDelete.as_view(), name='project-delete'),

    path('<int:project_pk>/files/add/', ProjectFileCreate.as_view(), name='file-create'),
    path('<int:project_pk>/files/<int:file_pk>/update/', ProjectFileUpdate.as_view(), name='file-update'),
    path('<int:project_pk>/files/<int:file_pk>/delete/', ProjectFileDelete.as_view(), name='file-delete'),
]
urlpatterns=[
路径(“”,ProjectList.as_view(),name='projects-list'),
路径('create/',ProjectCreate.as_view(),name='project-create'),
路径(“/”,ProjectDetail.as_view(),name='project-detail'),
路径('/update/',ProjectUpdate.as_view(),name='project-update'),
路径('/delete/',ProjectDelete.as_view(),name='project-delete'),
路径('/files/add/',ProjectFileCreate.as_view(),name='file-create'),
路径('/files//update/',ProjectFileUpdate.as_view(),name='file-update'),
路径('/files//delete/',ProjectFileDelete.as_view(),name='file-delete'),
]

提前谢谢