我正在为志愿消防员创建一个可用性应用程序,并且无法在模板中显示其中一个模型字段
models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Team(models.Model):
name = models.CharField('Name', max_length = 200)
def __str__(self):
return self.name
class Firefighter(models.Model):
RANKS = (
('CFO', 'Chief Fire Officer'),
('DCFO','Deputy Chief Fire Officer'),
('SSO', 'Senior Station Officer'),
('SO', 'Station Officer'),
('SFF', 'Senior Firefighter'),
('QFF', 'Qualified Firefighter'),
('FF', 'Firefighter'),
('RFF', 'Recruit Firefighter'),
('OS', 'Operational Support'),
)
STATUS_OPTIONS = (
('AV', 'Available'),
('OD', 'On Duty'),
('UN', 'Unavailable'),
('LV', 'Leave'),
)
rank = models.CharField("Rank", max_length = 50 , choices=RANKS, default='RFF')
first_name = models.CharField("First Name", max_length = 200)
last_name = models.CharField("Last Name", max_length = 200)
start_date = models.DateField(name="Start Date")
status = models.CharField("Status", max_length = 20 , choices=STATUS_OPTIONS, default='Available')
driver = models.BooleanField('Driver', default=False)
officer = models.BooleanField('Officer Qualified', default=False)
teams = models.ManyToManyField(Team)
def __str__(self):
return self.first_name + ' ' + self.last_name
class Meta:
verbose_name = "Firefighter"
verbose_name_plural = "Firefighters"
views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.forms import ModelForm
from .models import Firefighter
from django.http import HttpResponseRedirect
# Create your views here.
@login_required(login_url='/login/')
def status(request):
firefighters = Firefighter.objects.all()
context = {
'firefighters': firefighters,
'availableCount': firefighters.filter(status='AV').count() + firefighters.filter(status='OD').count(),
'leaveCount': firefighters.filter(status='LV').count(),
'unAvCount': firefighters.filter(status='UN').count()
}
return render(request, 'status.html', context)
@login_required(login_url='/login/')
def details(request, id):
class ChangeStatus(ModelForm):
class Meta:
model = Firefighter
fields = ['status']
form = ChangeStatus()
firefighter = Firefighter.objects.get(id=id)
if request.method == 'POST':
form = ChangeStatus(request.POST, instance=firefighter)
if form.is_valid():
form.save()
return HttpResponseRedirect('/status/')
else:
form = ChangeStatus()
context = {
'firefighter': firefighter,
'form': form
}
return render(request, 'details.html', context)
def members(request):
firefighters = Firefighter.objects.all().order_by('rank')
context = {
'firefighters': firefighters,
}
return render(request, 'members.html', context)
以及无法正确呈现的members.html
{% extends "base.html" %}
{% block content %}
<ul>
{% for firefighter in firefighters %}
<li> {{firefighter.rank}} {{firefighter.first_name}} {{firefighter.last_name}} {{firefighter.start_date}}</li>
{% endfor %}
</ul>
{% endblock content %}
member.html将正确呈现firefighter.first_name
和firefighter.last_name
,但firefighter.start_date
没有显示任何内容,即使每个消防员在数据库中都有指定的开始日期。
关于为什么的任何想法?
奖金问题:在查询数据库时,有没有办法让我对排名进行排序?
答案 0 :(得分:1)
您为start_date定义字段:
start_date = models.DateField(name="Start Date")
应该是:
start_date = models.DateField("Start Date")
Date_Field中的第一个arg是verbose_name,它将使用更可接受的名称snake_case版本自动设置名称。
答案 1 :(得分:1)
就您的订购而言,尝试使用将要排序的值为您的选择设置变量。
CFO = ‘A’
DCFO = ‘B’
...
RANK_CHOICES = (
(CFO, ‘Chief Fire Officer’),
(DCFO, ‘Deputy Chief Fire Officer’)
(...)
)
如果您一直想按排名查询结果:
class Meta:
ordering = [‘rank’]
如果没有,请使用您当前的查询。
如Zev所述,“name ='label'”对任何字段均无效。它是verbose_name=‘label
。根据字段,您可以将标签作为引号中的第一个参数,但是必须使用其他字段verbose_name。