Issue
I am trying to understand why Django its not printing on my HTML page, the user db its printing just fine but my ticket db its not being printed, it actually creates the space to allocate the information, as its shown on the image, but it doesn't fill the information on it.
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)
home.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 %}
Actual page
If any more information is needed please just comment it and i shall provide it
Solution
In the view function, I would rename the ticket
variable to tickets
(plural), so it's more obvious that it's actually a list of tickets. Having variable names that actually match the expected content is really helpful:
def dashboard(request):
tickets = Userdb.objects.filter(userid = request.user.id)
context = {'tickets': tickets}
return render(request,'dashboard/home.html', context)
But the real problem is that the html template assumes ticket
is each separate ticket, not the list itself:
{% 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 %}
Assuming that you made my recommended change above, you can change just the for
loop tag to this:
{% for ticket in tickets %}
Or if you don't want to make that change, you can change {{ticket.ticketid}}
to {{Userdb.ticketid}}
and all of the other similar variables.
Answered By - John Gordon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.