【发布时间】:2020-04-23 00:01:11
【问题描述】:
当前路径:
ticket-strap/
ticketstrap/
templates/
home.html (home.html file in question)
index.html
tickets/
__pycache__
migrations
templates/
__init__.py
admin.py
apps.py
models.py (models file in question)
tests.py
urls.py (urls file in question)
views.py (views file in question)
ticketstrap/
__pycache__
__init__.py
asgi.py
settings.py
urls.py (urls file in question)
views.py
wsgi.py
代码如下:
urls.py(来自票务应用)
from django.contrib import admin
from django.conf.urls import url
from django.conf.urls import include
from django.urls import path
from . import views
app_name = 'ticketstrap'
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name="homepage"),
path('home/', views.home, name="home"),
path('tickets/', include('tickets.urls')),
path('accounts/', include('accounts.urls'))
# url(r'^$', views.home, name="homepage"),
# url(r'^home/', views.home, name="home"),
# url(r'^tickets/', include('tickets.urls')),
# url(r'^accounts/', include('accounts.urls'))
]
urls.py(来自门票应用程序)
from django.conf.urls import url
from django.urls import path
from . import views
app_name = 'tickets'
urlpatterns = [
path('', views.tickets, name="tickets"),
path('new_ticket_submission/', views.new_ticket_submission, name="new_ticket_submission"),
path('<int:ticket_id>/', views.ticket_details, name="ticket_details")
# url(r'^$', views.tickets, name="tickets"),
# url(r'^new_ticket_submission/$', views.new_ticket_submission, name="new_ticket_submission")
]
models.py(来自门票应用)
from django.db import models
class Ticket(models.Model):
customer_name = models.CharField(max_length=50)
customer_email = models.CharField(max_length=100)
ticket_subject = models.CharField(max_length=200)
ticket_type = models.CharField(max_length=20)
ticket_status = models.CharField(max_length=30)
ticket_priority = models.CharField(max_length=30)
ticket_description = models.TextField()
ticket_tags = models.CharField(max_length=50)
# slug = models.SlugField()
# submit_date = models.DateTimeField(auto_now_add=True)
# submit_date = models.DateTimeField('Date Submitted')
def __str__(self):
return "status: {} | Subject: {} | Name: {}".format(self.ticket_status, self.ticket_subject, self.customer_name)
views.py(来自门票应用程序)
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from tickets.models import Ticket
@login_required(login_url="/accounts/login")
def home(request):
return render(request, "home.html")
def ticket_status_count(request):
open_count = Ticket.objects.filter(ticket_status='open').count()
in_progress_count = Ticket.objects.filter(ticket_status='inProgress').count()
pending_count = Ticket.objects.filter(ticket_status='pending').count()
resolved_count = Ticket.objects.filter(ticket_status='resolved').count()
closed_count = Ticket.objects.filter(ticket_status='closed').count()
waiting_on_customer_count = Ticket.objects.filter(ticket_status='waitingOnCustomer').count()
waiting_on_third_party_count = Ticket.objects.filter(ticket_status='waitingOnThirdParty').count()
return render(request, 'home.html', {'open_count': open_count})
home.html
{% extends "index.html" %}
{% block content %}
<!-- Today's Tasks Header -->
<header id="header">
<div class="container">
<div class="row">
<div class="col-md-6">
<h2><svg class="bi bi-calendar" width="1em" height="1em" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M16 2H4a2 2 0 00-2 2v12a2 2 0 002 2h12a2 2 0 002-2V4a2 2 0 00-2-2zM3 5.857C3 5.384 3.448 5 4 5h12c.552 0 1 .384 1 .857v10.286c0 .473-.448.857-1 .857H4c-.552 0-1-.384-1-.857V5.857z" clip-rule="evenodd"/>
<path fill-rule="evenodd" d="M8.5 9a1 1 0 100-2 1 1 0 000 2zm3 0a1 1 0 100-2 1 1 0 000 2zm3 0a1 1 0 100-2 1 1 0 000 2zm-9 3a1 1 0 100-2 1 1 0 000 2zm3 0a1 1 0 100-2 1 1 0 000 2zm3 0a1 1 0 100-2 1 1 0 000 2zm3 0a1 1 0 100-2 1 1 0 000 2zm-9 3a1 1 0 100-2 1 1 0 000 2zm3 0a1 1 0 100-2 1 1 0 000 2zm3 0a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd"/>
</svg><small> Today's Tasks</small></h2>
</div>
</div>
</header>
<section id="breadcrumb">
<div class="container-fluid">
<ol class="breadcrumb">
<li class="active">Dashboard</li>
</ol>
</div>
</section>
<section id="main">
<div class="container">
<div class="row">
<!-- Left Dashboard -->
<div class="col-md-3">
<div class="list-group">
<a href="/index/" class="list-group-item list-group-item-action active main-color-bg"><svg class="bi bi-display-fill" width="1em" height="1em" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M7.75 15.5c.167-.333.25-.833.25-1.5h4c0 .667.083 1.167.25 1.5H13a.5.5 0 010 1H7a.5.5 0 010-1h.75z"/>
<path fill-rule="evenodd" d="M15.991 5H4c-.325 0-.502.078-.602.145a.758.758 0 00-.254.302A1.46 1.46 0 003 6.01V12c0 .325.078.502.145.602.07.105.17.188.302.254a1.464 1.464 0 00.538.143L4.01 13H16c.325 0 .502-.078.602-.145a.758.758 0 00.254-.302 1.464 1.464 0 00.143-.538L17 11.99V6c0-.325-.078-.502-.145-.602a.757.757 0 00-.302-.254A1.46 1.46 0 0015.99 5zM16 4H4C2 4 2 6 2 6v6c0 2 2 2 2 2h12c2 0 2-2 2-2V6c0-2-2-2-2-2z" clip-rule="evenodd"/>
<path d="M4 6h12v6H4z"/>
</svg> Dashboard</a>
<a href="/tickets/" class="list-group-item list-group-item-action"><svg class="bi bi-tag-fill" width="1em" height="1em" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M4 3a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l4.586-4.586a1 1 0 000-1.414l-7-7A1 1 0 008.586 3H4zm4 3.5a1.5 1.5 0 11-3 0 1.5 1.5 0 013 0z" clip-rule="evenodd"/>
</svg> Tickets <span class="badge badge-pill badge-primary">change_num</span></a>
<a href="/contacts/" class="list-group-item list-group-item-action"><svg class="bi bi-person-fill" width="1em" height="1em" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M5 16s-1 0-1-1 1-4 6-4 6 3 6 4-1 1-1 1H5zm5-6a3 3 0 100-6 3 3 0 000 6z" clip-rule="evenodd"/>
</svg> Contacts</a>
<a href="/companies/" class="list-group-item list-group-item-action"><svg class="bi bi-building" width="1em" height="1em" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M17.285 2.089a.5.5 0 01.215.411v15a.5.5 0 01-.5.5h-3a.5.5 0 01-.5-.5V16h-1v1.5a.5.5 0 01-.5.5H3a.5.5 0 01-.5-.5v-6a.5.5 0 01.418-.493l5.582-.93V5.5a.5.5 0 01.324-.468l8-3a.5.5 0 01.46.057zM9.5 5.846V10.5a.5.5 0 01-.418.493l-5.582.93V17h8v-1.5a.5.5 0 01.5-.5h2a.5.5 0 01.5.5V17h2V3.221l-7 2.625z" clip-rule="evenodd"/>
<path fill-rule="evenodd" d="M8.5 17.5v-7h1v7h-1z" clip-rule="evenodd"/>
<path d="M4.5 13h1v1h-1v-1zm2 0h1v1h-1v-1zm-2 2h1v1h-1v-1zm2 0h1v1h-1v-1zm6-10h1v1h-1V5zm2 0h1v1h-1V5zm-4 2h1v1h-1V7zm2 0h1v1h-1V7zm2 0h1v1h-1V7zm-2 2h1v1h-1V9zm2 0h1v1h-1V9zm-4 0h1v1h-1V9zm0 2h1v1h-1v-1zm2 0h1v1h-1v-1zm2 0h1v1h-1v-1zm-4 2h1v1h-1v-1zm2 0h1v1h-1v-1zm2 0h1v1h-1v-1z"/>
</svg> Companies</a>
<a href="/reports/" class="list-group-item list-group-item-action"><svg class="bi bi-graph-up" width="1em" height="1em" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M2 2h1v16H2V2zm1 15h15v1H3v-1z"/>
<path fill-rule="evenodd" d="M16.39 6.312l-4.349 5.437L9 8.707l-3.646 3.647-.708-.708L9 7.293l2.959 2.958 3.65-4.563.781.624z" clip-rule="evenodd"/>
<path fill-rule="evenodd" d="M12 5.5a.5.5 0 01.5-.5h4a.5.5 0 01.5.5v4a.5.5 0 01-1 0V6h-3.5a.5.5 0 01-.5-.5z" clip-rule="evenodd"/>
</svg> Reports</a>
<a href="/adminSettings/" class="list-group-item list-group-item-action"><svg class="bi bi-gear-fill" width="1em" height="1em" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M11.405 3.05c-.413-1.4-2.397-1.4-2.81 0l-.1.34a1.464 1.464 0 01-2.105.872l-.31-.17c-1.283-.698-2.686.705-1.987 1.987l.169.311c.446.82.023 1.841-.872 2.105l-.34.1c-1.4.413-1.4 2.397 0 2.81l.34.1a1.464 1.464 0 01.872 2.105l-.17.31c-.698 1.283.705 2.686 1.987 1.987l.311-.169a1.464 1.464 0 012.105.872l.1.34c.413 1.4 2.397 1.4 2.81 0l.1-.34a1.464 1.464 0 012.105-.872l.31.17c1.283.698 2.686-.705 1.987-1.987l-.169-.311a1.464 1.464 0 01.872-2.105l.34-.1c1.4-.413 1.4-2.397 0-2.81l-.34-.1a1.464 1.464 0 01-.872-2.105l.17-.31c.698-1.283-.705-2.686-1.987-1.987l-.311.169a1.464 1.464 0 01-2.105-.872l-.1-.34zM10 12.93a2.929 2.929 0 100-5.858 2.929 2.929 0 000 5.858z" clip-rule="evenodd"/>
</svg> Admin</a>
</div>
</div>
<!-- Unresolved, Due Today, and Open Cards -->
<div class="col-md-9">
<div class="card-deck">
<div class="card">
<div class="card-body">
<h3 class="card-title">Open</h3>
<h1 class="card-text">
{{open_count}}
</h1>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
<div class="card">
<div class="card-body">
<h3 class="card-title">On Hold</h3>
<h1 class="card-text">12</h1>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
<div class="card">
<div class="card-body">
<h3 class="card-title">Unresolved</h3>
<h1 class="card-text">42</h1>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
</div>
</div> <!-- Row collapse -->
<!-- <div class="col-md-9">
</div> -->
</div>
</section>
{% endblock %}
在 views.py 中,ticket_status_count 计算我的票证管理系统中的每个票证实例。在这种情况下,我想计算有多少票被标记为“打开”,分配给变量open_count。然后我尝试从views.py 中获取open_count 并将其显示在home.html 上。在我的 Django 数据库中,我已经确认有 2 张票标记为“打开”,Ticket.objects.filter(ticket_status='open').count() 确实返回了正确的数字,这是通过 Python Shell 中的测试确定的。但是,我的浏览器中的 home.html 中没有显示数字 2。
到目前为止我已经尝试过:
在views.py中我只试过return open_count。
在 home.html 我试过了:
{% for status in open_count %}
{{status}}
{ % endfor %}
和
{% if open_count %}
{{open_status}}
{% endif %}
【问题讨论】:
-
上下文变量名为“open_count”,但您使用的是“open_status”
-
你能分享这两个视图的
urls.py吗? -
在初始代码中,正确的是,模板中间的
{{open_count}}- 这应该可以工作。但在后面的示例中使用了open_status,这一事实让人怀疑视图和模板的正确版本是否曾经一起使用过。 -
@IainShelvington 我很抱歉,那些本来是 open_count。我正在重新输入代码,并且一定把我的变量搞混了。我会立即编辑帖子
-
@BrianLee 你能分享这两个视图的
urls.py以及你当前的路径吗?
标签: python django django-models django-templates django-views