我试图理解为什么Django没有在我的HTML页面上打印,用户数据库打印得很好,但我的ticket数据库没有被打印,它实际上创建了空间来分配信息,如图所示,但它没有填充信息。
views.py
#The actual page
def dashboard(request):
ticket = Userdb.objects.filter(userid = request.user.id)
context = {'ticket': ticket}
return render(request,'dashboard/home.html', context)
#Data in the ticket
def ticketr(request):
data = {}
if request.method == 'POST':
if request.POST.get('nomctt') and request.POST.get('tiposerv') and request.POST.get('areaserv') and request.POST.get('tickettext'):
table = Userdb()
table.nomctt = request.POST.get('nomctt')
table.numctt = request.POST.get('numctt')
table.tiposerv = request.POST.get('tiposerv')
table.areaserv = request.POST.get('areaserv')
table.tickettext = request.POST.get('tickettext')
table.userid = request.user
table.save()
data['msg'] = 'Ticket registrado!'
data['class'] = 'alert-success'
return render(request,'dashboard/home.html', data)
else:
data['msg'] = 'Preencha todos os campos!'
data['class'] = 'alert-danger'
return render(request,'dashboard/ticket.html',data)
字符串
首页.html
{% block content %}
{% if request.user.is_authenticated %}
<nav class="navbar">
<div class="navbar__container">
<a href="/" id="navbar__logo"><img src="{% static 'images/logo.png' %}" style="width:180px;height:100px;"></a>
<div class="navbar__toggle" id="mobile-menu">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
<ul class="navbar__menu">
<ul class="navbar__menu">
<li class="navbar__item">
<a href="/" class="navbar__links">
{{ user.username }}
</a>
</li>
<li class="navbar__btn">
<a href="/loginv" class="button">
Logar
</a>
</li>
</ul>
</div>
</nav>
<a href="/dashboard/ticket/">Abra um Ticket</a>
</div>
<br>
<div class="container bootdey">
<div class="col-md-12">
<div class="col-md-4">
<div class="portlet light profile-sidebar-portlet bordered">
<div class="profile-usertitle">
<div class="profile-usertitle-name"> {{ user.username }} </div>
<div class="profile-usertitle-job"> Developer </div>
</div>
<div class="profile-usermenu">
<ul class="nav">
<li class="active">
<a href="#">
<i class="icon-home"></i> Ticket List </a>
</li>
<li>
<a href="#">
<i class="icon-info"></i> Configurations </a>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-8">
<div class="portlet light bordered">
<div class="portlet-title tabbable-line">
<div class="caption caption-md">
<i class="icon-globe theme-font hide"></i>
<span class="caption-subject font-blue-madison bold uppercase">Ticket List</span>
</div>
</div>
<div class="portlet-body">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Column heading</th>
<th>Column heading</th>
<th>Column heading</th>
</tr>
</thead>
<tbody>
{% for Userdb in ticket %}
<tr class="active">
<th scope="row">{{ticket.ticketid}}</th>
<td>{{ticket.nomctt}}</td>
<td>{{ticket.numctt}}</td>
<td>{{ticket.tickettext}}</td>
</tr>
{% endfor %}
{% else %}
Você não tem acesso a essa área!
{% endif %}
{% endblock %}
型
实际页面
的数据
如果需要任何更多的信息,请只是评论它,我将提供它
1条答案
按热度按时间mi7gmzs61#
在view函数中,我会将
ticket
变量重命名为tickets
(复数),这样更明显的是它实际上是一个tickets的 list。拥有与预期内容实际匹配的变量名真的很有帮助:字符串
但真实的问题是html模板假定
ticket
是每个单独的票,而不是列表本身:型
假设你做了我上面推荐的修改,你可以只把
for
循环标记改为:型
或者,如果你不想做这样的改变,你可以把
{{ticket.ticketid}}
改为{{Userdb.ticketid}}
,以及其他类似的变量。