【发布时间】: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