Django不能在HTML上打印

liwlm1x9  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(110)

我试图理解为什么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 %}


实际页面


的数据
如果需要任何更多的信息,请只是评论它,我将提供它

mi7gmzs6

mi7gmzs61#

在view函数中,我会将ticket变量重命名为tickets(复数),这样更明显的是它实际上是一个tickets的 list。拥有与预期内容实际匹配的变量名真的很有帮助:

def dashboard(request):
    tickets = Userdb.objects.filter(userid = request.user.id)
    context = {'tickets': tickets}
    return render(request,'dashboard/home.html', context)

字符串
但真实的问题是html模板假定ticket是每个单独的票,而不是列表本身:

{% 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 %}


假设你做了我上面推荐的修改,你可以只把for循环标记改为:

{% for ticket in tickets %}


或者,如果你不想做这样的改变,你可以把{{ticket.ticketid}}改为{{Userdb.ticketid}},以及其他类似的变量。

相关问题