[英]In django, how can I retrieve a value from db into a custom field template?
[英]how to update the value of a db field in django template
我的 models.py 中有一个名为status
的变量,如下所示:
class message(models.Model):
class Choices_status(models.TextChoices):
unread = 'Unread', "Unread"
processing = 'Processing', "Processing"
obsolete = 'Obsolete', "Obsolete"
status = models.CharField(max_length=25, choices=Choices_status.choices, default=Choices_status.unread, blank=True)
它指示消息的状态。 我想将它的值更改为Choices_status
的其他选择。
即在我显示messages
列表的模板内,如果选择了一条消息并在新页面上显示,我想将此变量的值更改为其他选项。
我所能找到的只是使用不适合我的情况的{% with %}
标签。
有任何想法吗?
更新:
我想从模板中更新数据库字段的值。 我没有使用 Django forms 和 class 视图。
您的代码应如下所示:
class message(models.Model):
UNREAD = 'Unread'
PROCESSING= 'Processing'
OBSOLETE= 'Obsolete'
MY_CHOICES = [
(UNREAD ,'Unread')
(PROCESSING,'Processing')
(OBSOLETE,'Obsolete')
]
status = models.CharField(
max_length=25,
choices=MY_CHOICES,
default=UNREAD,
blank=True
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.