【问题标题】:How to display the contents of a table as a table in django?如何在 django 中将表格的内容显示为表格?
【发布时间】:2019-07-25 11:51:27
【问题描述】:

我制作了一个简单的 django 应用程序。在我的 models.py 中,我定义了一个表格 -

class events(models.Model):
    id_campaign = models.CharField(default='newevent',max_length=100)
    status = models.CharField(default='100',max_length=100)
    name = models.CharField(default='100',max_length=100)

这是我的views.py -

from django.shortcuts import render

# Create your views here.

from django.views.generic import TemplateView

from django.shortcuts import render
from website.models import *
from website.models import events


from django.views.generic import ListView

class HomePageView(TemplateView):
    template_name = 'base.html'


class AboutPageView(TemplateView):
    template_name = 'about.html'

class ContactPageView(TemplateView):
    template_name = 'contact.html'

class Success(TemplateView):
    template_name = 'success.html'
    def MyView(self, request):
        query_results = events.objects.all()
        return render(request, self.template_name)

这就是success.html -

{% if user.is_authenticated %}
<header>
    <a href=" {% url 'logout'%}">Logout</a>
</header>

{% block content %}
<h2>you are logged in successfully. this is your personal space.</h2>

<table>
    <tr>
        <th>id_campaign</th>
        <th>status</th>
        <th>name</th>
   </tr>
    {% for item in query_results %}
    <tr>
        <td>{{ item.id_campaign }}</td>
        <td>{{ item.status }}</td>
        <td>{{ item.name }}</td>
    </tr>
    {% endfor %}
</table>

{% endblock %}

{% else %}

<h2>you are not logged in.</h2>

{%endif %}

我的问题是,一旦用户登录,他只能看到硬编码的列名,但列值不可见。就好像 {% for item in query_results %}。部分不工作。

我该怎么办?我想以表格的方式查看内容?

我还需要检查用户名并根据需要显示结果的用户,我该怎么做?

if request.user.is_authenticated(): 
  username = request.user.username

如果用户名是“xyz”,那么我想显示来自 query_results 的值,如果用户名是“abc”,那么我不想显示。

附: - 我对 django 完全陌生

【问题讨论】:

    标签: django django-models django-forms django-templates


    【解决方案1】:

    您以错误的方式使用TemplateView。 您需要覆盖 TemplateView 中的 get_context_data() 方法。

    class Success(TemplateView):
        template_name = 'success.html'
    
        def get_context_data(self, **kwargs):
            context = super(Success, self).get_context_data(**kwargs)
            query_results = events.objects.all()
            context.update({'query_results': query_results})
            return context
    

    现在应该可以正常工作了。

    【讨论】:

    • 非常感谢!有效。我只是从 django 开始。我从字面上开始2天前。 :)
    • 嘿兄弟!如果我需要检查用户名并根据需要显示结果的用户,我该怎么做?如果 request.user.is_authenticated(): 用户名 = request.user.username。如果用户名是“xyz”则显示一些内容,如果用户名是“abc”则显示其他内容。
    【解决方案2】:

    您误解了基于类的视图的工作原理。您的 MyView 方法永远不会被调用(尽管请注意,即使它被调用了,它实际上仍然没有执行任何操作来将 query_results 发送到模板)。

    你需要定义get_context_data方法,它返回一个字典:

    class Success(TemplateView):
        template_name = 'success.html'
        def get_context_data(self, *args, **kwargs):
            query_results = events.objects.all()
            return {'query_results': query_results}
    

    但请注意,更好的方法是使用适当的通用视图;你想显示一个项目列表,所以使用 ListView。

    class Success(ListView):
        template_name = 'success.html'
        model = events
    

    并在您的模板中执行{% for item in events_list %}

    【讨论】:

    • 非常感谢!有效。我只是从 django 开始。我从字面上开始2天前。 :)
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2016-07-31
    相关资源
    最近更新 更多