python-3.x - strftime 模型对象并在 html 模板中显示
问题描述
美好的一天,我想对创建的模型实例进行 strftime 并将其显示在 HTML 模板中(作为 transaction_id)。但我似乎没有做对。谢谢你的帮助。
模型.py
class Order(models.Model):
user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
address = models.CharField(max_length=250)
phone_number = models.CharField(max_length=20)
city = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
paid = models.BooleanField(default=False)
braintree_id = models.CharField(max_length=150, blank=True)
coupon = models.ForeignKey(Coupon, related_name='orders', null=True, blank=True, on_delete=models.SET_NULL)
discount = models.IntegerField(default=0, validators=[
MinValueValidator(0),
MaxValueValidator(100)
])
视图.py
def order_list(request):#datetime.now().strftime("%Y%m%d%H%M%S")
transaction_id = Order.objects.get(created)
orders = Order.objects.all()
current_user = request.user
success = Order.objects.filter(user=current_user.id).filter(paid=True)
fail = Order.objects.filter(user=current_user.id).filter(paid=False)
return render(request, 'orders/order/order_list.html', {
'success': success,
'fail': fail,
'current_user': current_user,
'orders':orders,
'transaction_id':transaction_id,
})
html
<p class="card-text">
<mark style="color: whitesmoke; background-color: brown;border-radius: 3px;font-weight: bold;">{{transaction_id}}</mark>
</p>
解决方案
好吧,这就是我为解决这个问题所做的,我在我的 models.py 中添加了 strftime 函数
模型.py
def htmldisplaytime(self):
time = self.created.strftime("%Y%m%d%H%M%S")
return time
推荐阅读
- active-directory - 为什么客户总是希望通过 AD 和使用 AD 安全组的安全创建用户帐户?
- visual-studio - 我无法调试 Xamarin.Android 应用程序
- javascript - 在 redux 中调度存储变量时,React 组件不会重新渲染
- html - 手风琴内的超链接是半透明的(仅限 HTML CSS)
- python - 如何执行依赖于在另一个 python gui 脚本中提取文件名的 python 脚本?
- javascript - 在 Django 模板中使用 ViewerJS 显示 office 文件或 pdf 文件
- javascript - 使用 HTML 表将用户输入或响应保存到本地存储
- c# - .Net Core 使用 Serilog 将日志记录到 PostgreSQL 不工作
- python - 如何在 Python 中为函数找到最合适的参数
- java - 如何在打印时更改树形图中的变量?