[英]How to get the image url from an ImageField in a Django model with a JsonResponse?
I have this Model in a Django app我在 Django 应用程序中有这个 Model
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
say = models.CharField(max_length=250)
date = models.DateTimeField(auto_now_add=True)
photo = models.ImageField(null = True, blank = True)
def serialize(self):
return {
"user": self.user.username,
"say": self.say,
"date": self.date.strftime("%a-%d-%b-%Y , %H:%M:%S %p"),
"photo": self.photo
}
and this function in the views.py和views.py中的这个function
@login_required(login_url = 'login')#redirect when user is not logged in
def all_comments(request):
all_comments = Comment.objects.order_by("-date").all()
return JsonResponse([comment.serialize() for comment in all_comments], safe=False)
and finaly this part of code in my javascript file最后这部分代码在我的 javascript 文件中
document.addEventListener('DOMContentLoaded', function() {
fetch('/all_comments')
.then(response => response.json())
.then(all_comments => {
do somethings with the data...
and when i run this i'm getting the error:当我运行它时,我得到了错误:
TypeError: Object of type ImageFieldFile is not JSON serializable TypeError:ImageFieldFile 类型的 Object 不是 JSON 可序列化的
If i remove the line如果我删除线
"photo": self.photo “照片”:self.photo
from the serialize function inside the mode everything goes fine and im getting the data that i need.从模式内的序列化 function 一切顺利,我得到了我需要的数据。 How can i retrieve images url, with the JsonResponse as well, in order to display it with the other data?
如何使用 JsonResponse 检索图像 url,以便与其他数据一起显示? Ihave tryied many things i found here and there but i cant find any solution.
我已经尝试了很多我在这里和那里找到的东西,但我找不到任何解决方案。 Thanks so much in advance
非常感谢提前
Adding .url
after photo will give you the photo's URL as a string, add like so:在照片之后添加
.url
会给你照片的 URL 作为字符串,像这样添加:
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
say = models.CharField(max_length=250)
date = models.DateTimeField(auto_now_add=True)
photo = models.ImageField(null = True, blank = True)
def serialize(self):
return {
"user": self.user.username,
"say": self.say,
"date": self.date.strftime("%a-%d-%b-%Y , %H:%M:%S %p"),
"photo": self.photo.url
}
try this尝试这个
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
say = models.CharField(max_length=250)
date = models.DateTimeField(auto_now_add=True)
photo = models.ImageField(upload_to='myphotos',null = True, blank = True)
def serialize(self):
if self.photo:
return {
"user": self.user.username,
"say": self.say,
"date": self.date.strftime("%a-%d-%b-%Y , %H:%M:%S %p"),
"photo": self.photo.url
}
return {
"user": self.user.username,
"say": self.say,
"date": self.date.strftime("%a-%d-%b-%Y , %H:%M:%S %p")
}
or或者
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
say = models.CharField(max_length=250)
date = models.DateTimeField(auto_now_add=True)
photo = models.ImageField(upload_to='myphotos',null = True, blank = True)
def serialize(self):
return {
"user": self.user.username,
"say": self.say,
"date": self.date.strftime("%a-%d-%b-%Y , %H:%M:%S %p"),
"photo": self.photo.url if self.photo else None
}
Note is it important to add upload_to
to the FileField or ImageField
.do not forget to run migrations an migrate.请注意,将
upload_to
添加到FileField or ImageField
很重要。不要忘记运行迁移和迁移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.