gpt4 book ai didi

python - 在 Django 中将数据库对象转换为字符串

转载 作者:太空宇宙 更新时间:2023-11-04 03:57:00 24 4
gpt4 key购买 nike

模型.py

class UserProfile(models.Model):
user = models.OneToOneField(User)
security_question = models.CharField('Question', max_length=255, null=True, blank=True)
security_answer = models.CharField('Answer', max_length=255, null=True, blank=True)
phone_daytime = models.CharField('Phone daytime', max_length=50, null=True, blank=True)
phone_mobile = models.CharField('Phone mobile', max_length=50, null=True, blank=True)
work_street = models.CharField('Street', max_length=255, null=True, blank=True)
work_suburb = models.CharField('Suburb', max_length=200, null=True, blank=True)
work_state = models.CharField('State', max_length=100, null=True, blank=True)
work_postcode = models.CharField('Postcode', max_length=20, null=True, blank=True)
work_country = models.CharField('Country', max_length=200, null=True, blank=True)

View .py

def method(request):
''''
profile = UserProfile.objects.get(user=user)
'''''
return render(request,'index.html',{'profile':profile})

如何将数据库字段的work_street、work_suburb、work_state、work_postcode、work_country转换成字符串并在模板中渲染。

最佳答案

您可以像这样创建 index.html 以将其呈现为 html

index.html

<body>
<p>{{ profile.user.username }}</p>
<p>{{ profile.security_question }}</p>
<p>{{ profile.work_street}}</p>
<p>{{ profile.work_suburb}}</p>
<p>{{ profile.work_state}}</p>
<p>{{ profile.work_postcode}}</p>
<p>{{ profile.work_country }}</p>
..... etc

<script>
var address = "{{ profile.work_street }}{{profile.work_suburb}}{{profile.work_postcode}}";
alert(address);
</script>
</body>

关于python - 在 Django 中将数据库对象转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17788991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com