Django

2015-11-22 9 views
5

के साथ बैकएंड में जेनरेट की गई छवि को प्रदर्शित करने के लिए कैसे मैं django के लिए नया हूं और मैं छवि प्रदर्शन समस्या से उलझन में हूं। अब मेरे पास बैकएंड में उत्पन्न शब्द-क्लाउड की एक छवि है (मान लें, topicWords.py) और मुझे इससे निपटने के लिए गर्म पता नहीं है। (1) मैं इसे मॉडल UserProfile के छवि फ़ील्ड में कैसे संग्रहीत कर सकता हूं? models.py में, मेरे पास है:Django

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

(2) क्या MEDIA_ROOT की स्थापना और हो MEDIA_URL चाहिए:

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

यह सही सीधे की तरह करने के लिए है?

(3) मुझे इसे .html में कैसे प्रदर्शित करना चाहिए?

<img src = "???"> 

बहुत बहुत धन्यवाद!

+0

किस प्रकार करता है 'है wc'? क्या यह पथ पथ या ऑब्जेक्ट जैसी फ़ाइल है? –

+0

@ HåkenLid यह एक छवि वस्तु है। – yobichi

उत्तर

0

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 का उल्लेख कर सकते हैं।

+0

आपके उत्तर के लिए धन्यवाद! तो मेरे models.py में मुझे छवि को पिछले पते पर अपलोड नहीं करना चाहिए? क्या मुझे इसे MEDIA_ROOT के साथ सेट करना चाहिए? – yobichi

+0

@yobichi - अगर 'upload_to =" foo "', तो फ़ोटो 'MEDIA_ROOT' की "foo" उपनिर्देशिका में जाएगी। – JRodDynamite

+0

समझ गया! तो अंत में कोड user.userprofile.tagcloud।यूआरएल किसी निश्चित उपयोगकर्ता का सही चयन कर सकता है, यहां तक ​​कि सभी छवियां सैम निर्देशिका में हैं? (मैं आपके लिए वोट देना चाहता हूं लेकिन लगता है कि मैं नहीं कर सकता ....) – yobichi

0

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

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.

+0

आपके उत्तर के लिए धन्यवाद! लेकिन शायद यह उपयोगकर्ता.userprofile.tagcloud.save ('filename.png', content = wc, True) होना चाहिए? लेकिन मुझे अभी भी एक त्रुटि मिलती है जो मॉडल बचत में सम्मिलित और अद्यतन दोनों को मजबूर नहीं कर सकती है। क्या तुम्हारे पास कोई और योजनायें हैं? – yobichi

+0

यदि मॉडल मॉडल अभी तक डेटाबेस में सहेजा नहीं गया है तो आपको शायद 'save = False'' का उपयोग करना चाहिए। –

+0

मैंने 'PIL.Image' ऑब्जेक्ट को सहेजने के लिए कुछ कदम जोड़े हैं। आप इस उत्तर में एक समान प्रश्न के बारे में अधिक जानकारी प्राप्त कर सकते हैं: http://stackoverflow.com/a/4544525/1977847 –