How to display image created in backend using Django

I am new to django and am confused with the problem of displaying an image. Now I have a word cloud image generated in the backend (say, topicWords.py), and I don’t know how to deal with it. (1) How to save it in the image field of the UserProfile model? In models.py, I have:

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

Is it right to do the following:

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

(2) What should be the settings of MEDIA_ROOT and MEDIA_URL?

(3) How to display it in .html?

 <img src = "???"> 

Thank you very much!

+5
source share
3 answers

MEDIA_URL should look like this:

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

And add these lines at the end of your urls.py file:

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

You can access it in templates using:

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

If you want to set the ImageField path in Django manually, you can save the image file and then set the path value while saving the model instance. You can refer to this link for more information about this.

0
source

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() 
0
source

Django models.ImageField has a save() method inherited from FileField , which is usually the one you want to use when creating images on the server. This method takes care of upload_to , but also works with other file-based storage files.

 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() 

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

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

It takes two required arguments: name , which is the name of the file, and content , which is an object containing the contents of the files. The optional save argument determines whether the model instance is saved after changing the file associated with this field. Default is True .

Note that the content argument must be an instance of django.core.files.File , not a Pythons built-in file object.

0
source

Source: https://habr.com/ru/post/1236558/


All Articles