如何让通用 ListView 只显示用户的列表? [英] How to make generic ListView only show user's listing?

查看:31
本文介绍了如何让通用 ListView 只显示用户的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Django and I'm utilizing class-based views for the first time. I want to use the generic ListView to show a list of "tables" owned by a user. And so far I've gotten it to display ALL the tables in the database. But I only want it to show tables for the logged in user.

这是我的观点:

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

这就是我的模型的样子:

And this is what my model looks like:

from django.db import models
from django.contrib.auth.models import User

class Vtable(models.Model):
    user = models.ForeignKey(User)
    table_name = models.CharField(max_length=200)
    added_date = models.DateTimeField('date added')

class Vdata(models.Model):
    table_id = models.ForeignKey(Vtable)
    table_pk = models.IntegerField()
    column_1 = models.CharField(max_length=200)
    column_2 = models.CharField(max_length=200)
    added_date = models.DateTimeField('date added')

我承认,我不确定这条线在做什么:

I'll admit, I'm not sure what this line is doing:

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

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

But I suspect this is what I need to change? Or do I have to go into my template and do an if statement there? Maybe something like: {% 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 %}

推荐答案

也许,重写 get_queryset():

perhaps, overriding get_queryset():

def get_queryset(self):
    return self.model.objects.filter(user=self.request.user)

这篇关于如何让通用 ListView 只显示用户的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆