如何使通用的ListView只显示用户的列表?

2024-09-26 17:55:12 发布

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

我是Django新手,我第一次使用基于类的视图。我想使用通用的ListView来显示用户拥有的“表”列表。到目前为止,我已经让它显示数据库中的所有表。但我只想让它显示登录用户的表。在

我的观点是这样的:

from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from vtables.models import Vtable

class TableListView(generic.ListView):

    model = Vtable
    context_object_name = 'table_list'

    def get_context_data(self, **kwargs):
        context = super(TableListView, self).get_context_data(**kwargs)
        return context

我的模型是这样的:

^{pr2}$

我承认,我不知道这条线在干什么:

context = super(TableListView, self).get_context_data(**kwargs)

但我怀疑这就是我需要改变的?或者我必须进入我的模板并在那里做一个if语句?也许是这样的:{% if request.user == vtable.user %}?在

编辑:这是我的模板:

{% extends "base.html" %}
{% load staticfiles %}

{% block content %}
    {% if request.user.is_authenticated %}
        <img src="{{ request.user.profile.profile_image_url }}"/>
        <a href="/accounts/logout/" class="pull-right">Logout</a>

        {% if request.user.first_name or request.user.last_name %}
            {{ request.user.first_name }} {{ request.user.last_name }}
        {% else %}
            {{ request.user.username }}
        {% endif %}

        {% if request.user.profile.account_verified %}
            (verified)
        {% else %}
            (unverified)
        {% endif %}

        <h1>Tables</h1>
        <ul>
            {% for vtable in table_list %}
                <li>{{ vtable.user }}, {{ vtable.table_name }}</li>
            {% endfor %}
        </ul>
    {% else %}
        <a href="/accounts/login/" class="pull-right">Login</a>
    {% endif %}
{% endblock %}

Tags: djangonamefromimportselfdatagetif

热门问题