如何使用Django显示在后端生成的图像

How to display a image which is generated in the backend with Django

我是django新手,对图片显示问题一头雾水。 现在我有一个在后端生成的词云图像(比方说,topicWords.py),我不知道如何处理它。 (1) 如何将其存储在模型 UserProfile 的图像字段中?在 models.py 中,我有:

class UserProfile(models.Model):
user = models.OneToOneField(User)
tagcloud = models.ImageField(upload_to ='rap_song/raptle/pic/')

直接点赞对不对:

user.userprofile.tagcloud = wc #wc is the image generated

(2) MEDIA_ROOT和MEDIA_URL应该怎么设置?

(3) 我应该如何在.html?

中显示它
<img src = "???">

非常感谢!

MEDIA_URL应该是这样的:

MEDIA_ROOT='(the full path to the media folder)' (i.e: '/home/jason/work/project/media/')
MEDIA_URL='/media/'

并将这些行添加到 urls.py 文件的末尾:

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

您可以使用以下方法在模板中访问它:

<img src = "{{ user.userprofile.tagcloud.url }}">

如果你想在Django中手动设置一个ImageField路径,那么你可以保存图像文件,然后在保存模型实例时设置路径值。您可以参考此 link 了解更多信息。

settings.py

MEIDA_URL = '/pic/'
MEDIA_ROOT = os.path.join(REPOSITORY_ROOT, 'pic/')

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(REPOSITORY_ROOT, 'static/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
)

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static

urlpatterns = [
......
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += staticfiles_urlpatterns()

models.py

class UserProfile(models.Model):
user = models.OneToOneField(User)
tagcloud = models.ImageField(upload_to ='rapper/')

topicWords.py

d = path.dirname("/rapper/")


wc = WordCloud(background_color="white", max_words=2000, font_path='WeibeiSC-Bold.otf')
# generate word cloud
wc.generate_from_frequencies(word_tag)
# save to the "rapper/", right?
wc.to_file(path.join(d,"rapper1.png"))

userprofile.tagcloud = "rapper/rapper1.png"
userprofile.save()

Django 的 models.ImageField 有一个 save() 方法继承自 FileField,这通常是您在服务器上生成图像时要使用的方法。该方法负责 upload_to 并且还适用于其他文件存储后端。

from io import BytesIO
# Or if you use python 2:
# from StringIO import StringIO as BytesIO 
from PIL import Image
from django.core.files.base import ContentFile

wc = Image()  # Create the image somehow
filename = 'filename.png'  # Make a filename as well

in_memory_file = BytesIO()
wc.save(in_memory_file)
profile_image = ContentFile(in_memory_file.getvalue())
user.userprofile.tagcloud.save(name=filename, content=profile_image, save=False)
user.userprofile.save()

文档: https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.fields.files.FieldFile.save

FieldFile.save(name, content, save=True)

Takes two required arguments: name which is the name of the file, and content which is an object containing the file’s contents. The optional save argument controls whether or not the model instance is saved after the file associated with this field has been altered. Defaults to True.

Note that the content argument should be an instance of django.core.files.File, not Python’s built-in file object.