I'm trying to send images in a letter, but it doesn’t work out for me. I tuned {{ STATIC_URL }} but it did not help... How i can fix this?
In settings: STATIC_URL = 'http://10.0.50.42:8103/static/'
In HTML: <img src="{{ STATIC_URL }}dc/images/banner2.png">
With this setting, I started to receive a letter, but the images are not displayed there.
First of all you have to load static files using below command as first line of html...
{% load static %}
then use static as below in your <img>...
<img src="{% static "dc/images/banner2.png" %}">
In my knowledge you cannot just put the link of image in the template that is rendered in the email and expect that it will work. As you are sending the image it still resides on your machine/server. The image must be saved as in, in a model and then you can you use that Model object to send as attachment.
Lets assume the file model is just below:
class ImageFile(models.Model):
file = models.ImageField(upload_to='Images',verbose_name='File Path', max_length=400, null=True)
and the send_mail function looks like:
def send_mail():
file_obj = ImageFile.objects.get(file='banner2.png')
file_url = file_obj.file.path
msg = EmailMessage(subject=subject, body=email_body, from_email=[frommail],
to=[tomail], bcc=[bcc mails])
msg.attach_file(file_url)
msg.send()
Related
So I originally had the user create a post in admin and be able to leave image field blank and Django would set a default image.
My problem is;
If a user uploads an image, then deletes the image in admin, I get an error: The 'image' attribute has no file associated with it. when accessing solo.html in browser.
How can I make the default image reappear and specifically come from static folder?
My code:
settings.py
STATIC_URL = '/static/'
STATIC_DIRS = os.path.join(BASE_DIR, 'static')
models.py
# FIXME: If default image is changed to user's upload but then deleted. Make default image reappear.
# Recipe Field
class Recipe(models.Model):
title = models.CharField(max_length=200)
image = models.ImageField(upload_to='recipes/images/', blank=True)
def get_image(self):
if not self.image:
return f'{settings.STATIC_URL}default.png'
return self.image.url
solo.html
<h2>{{ recipe.title }}</h2>
<h3>{{ recipe.category }}</h3>
<h3>{{ recipe.meal }}</h3>
<img src="{{ recipe.image.url }}">
I'm just starting with Django so I apologize in advance if it's all a mess.
Thank you in advance!
What's happening is that the file itself is being deleted, but recipe.image is not being set to None.
I'm trying to display already stored database image in template. But here i'm getting only name of the image. How to solve this, Where i did mistake.
models.py
class Images(models.Model):
image = models.ImageField(upload_to='images', blank=True, null=True)
settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
def display(request):
myimages_objs = Images.objects.all().values()
for i in myimages_objs :
myimages_objs = i['image']
return render(request, 'index.html', {'myimages_obj ': myimages_objs })
index.html
<div class="divi">
<img src="{{ myimages_obj }}" alt="image">
</div>
I don't understand why you are returning the objects inside the for loop, this causes only a single image to be displayed in the template. Instead of doing this, you can pass all the image objects in the template, then loop through the objects in the template to display the images.
You can change your view as:
def display(request):
myimages_objs = Images.objects.all() # get all the images
return render(request, 'index.html', {'myimages_objs':myimages_objs})
Then in your template:
<div class="divi">
{% for item in myimages_objs %} //loop through all the images
<img src="{{ item.image.url }}" alt="image">
{% endfor %}
</div>
Edit:
Change the upload_to to upload_to="images/".
As far as I am concerned, your images are not being stored inside the images folder as you have missed the backslash(which represents a folder name). So, the images are being saved inside the media folder.
But the image url is something like project_path/media/images/something.jpg which cannot be found as the images are not being saved inside the images folder.
Change the field as stated for new images to be stored in the specified folder. For old images, copy them to the images folder manually.
I hope this will help.
From the past 2 days i have literally lost my patience trying to make Cloudfiles work for my project(using cumulus). Here are some of the issues:
1.) Sometimes when i upload any photo using admin(creating a model and registering with admin)... the photo looks like its uploaded but when i try to either access it using a view function by Photo.objects.all() or even going to the cloudfiles control panel...the image simply doesnt open up. I get a resource not found. I check and double check if the region(chicago is default for me) settings is screwing with me....but i don't think so.
2.) I have used collectstatic method to successfully collect all static files in a container and i am able to successfully serve them. infact when i click the link(say for example) - http://ed770b871265201bf471-14f03984d90730040890dd30a2d85248.r68.cf2.rackcdn.com/admin/css/base.css
I am able to see the results and i am sure u can see it too.
But when i am trying to use them by STATIC_URL in my templates - The admin pages don't have their css working but my login/home pages are perfectly being rendered with my styles.
Here are my settings file/my view functions and anything that is important -
STATIC_ROOT = ''
STATIC_URL = 'http://ed770b871265201bf471-14f03984d90730040890dd30a2d85248.r68.cf2.rackcdn.com/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'static'),
)
CUMULUS = {
'USERNAME': '***',
'API_KEY': '****',
'CONTAINER': 'photos',
'STATIC_CONTAINER':'static',
'SERVICENET': False, # whether to use rackspace's internal private network
'FILTER_LIST': [],
'TIMEOUT' : 60
}
DEFAULT_FILE_STORAGE = 'cumulus.storage.CloudFilesStorage'
STATICFILES_STORAGE = 'cumulus.storage.CloudFilesStaticStorage'
The model file part
class Photo(models.Model):
username = models.ForeignKey(User)
image = models.ImageField(upload_to='photos')
alt_text = models.CharField(max_length=255)
admin.site.register(Photo)
This is the view function as you requested kyle.
def profile_detail(request):
if request.user.is_authenticated():
username = request.user.get_username()
# userid = User.objects.get(username=username).values('__id')
userdetails = User.objects.filter(username=username)
photo = Photo.objects.get(username=request.user.id)
return render_to_response('profile_detail.html',{'userdetails':userdetails,'username':username,'photo':photo},
context_instance=RequestContext(request))
and the template for profile_detail -
{% extends 'profile.html' %}
{% load bootstrap_toolkit %}
{% block content %}
<img src="{{ photo.image.url }}" alt="{{ photo.alt_text }}" />
<br>
<p>{{ user.first_name }}</p>
<p>{{ user.last_name }}</p>
<p>{{ user.email }}</p>
{% endblock %}
I just now checked that i can view the image(No idea how) on the hosted site(production) but still can't do it in my dev environment.
Kyle can you please check if your testaccount has a picture of penguins in the 'MYProfile' page? :) Thanks for looking into it :)
The images are there, but possibly not where you expected them.
Link on your current site:
http://d12df125d01b8a258a3a-8112fdc02f7d385b44f56eb9e899d81c.r88.cf2.rackcdn.com/photos/Penguins.jpg
Where the image/file actually is:
http://d12df125d01b8a258a3a-8112fdc02f7d385b44f56eb9e899d81c.r88.cf2.rackcdn.com/photos%5CPenguins.jpg
The %5C is a \ rather than a /. This makes a difference as these are keys (key being the "path" and value being the file). You may want to check on how these were uploaded, and possibly normalize them to regular slashes (were these uploaded while you were on a Windows machine?).
I ahve this code, the trouble is few of the images are having names, but corresponding to the names the files are not present, how can i know if the images are being displayed, i mean some if statement of something like that.
<img src="{{ STATIC_URL }}images/{{web.image}}" />
The above code is used to render my django image in a template.
Thanks.
I would do this on the client side, with a javascript, just google something like: "javascript image loaded" or something.
Are you saying that the URL expanded from {{ STATIC_URL }}images/{{web.image}} is resulting in a 404 Not Found error? And you want to figure out which ones are doing this?
You'd really need to test those URLs in your view by using some python code - something from urllib maybe - but if that URL is on the same server as the one running the view then it might just hang on you.
If web is actually a database object that you are accessing (as your comment seems to point out), and that "image" is an ImageField, you could use the following :
{% if web.image %}
<img src="{% web.image.url %}" />
{% else %}
<img src="{{ STATIC_URL }}images/MyDefaultImage.png" />
{% endif %}
This way, the template will only provide the image if it exists, and provide an alternate image if not.
I apologize in advance if I misunderstood your question, I'm not so sure that I understood everything correctly.
It would be quite difficult to determine in the template whether the file actually exists or not. That would be the job of the view, assuming that {{web.image}} actually returns anything in the first place.
def my_view(request):
# some code here
import os
if os.path.exists(web.image_path): # for example
context = { 'web': web }
render_to_response('my_template.html', context, RequestContext(request)
This assumes you actually know what the full file system path to the image file is. That's not always going to be the case, especially with the staticfiles app in django 1.3.
However, I'd be a lot more concerned that sometimes images exist, and sometimes they don't. Why is that the case? Are images being deleted for some reason? If they are, you need a way of cleaning up the database.
Edit:
Since there doesn't yet seem to be an answer that you like, you should try this:
import os
class MyModel(models.Model):
image = models.Image...
# the rest of your fields
def get_image_url(self):
if os.path.exists(self.image.path):
return u'/%s/img/%s' % (self.app_label, self.image.filename)
return u'%s/img/default.jpg' % self.app_label
In your template, you then do the following:
<img src="{{ STATIC_URL }}{{ web.get_image_url }}" />
finally i have found a solution using sorl thumbnail : http://thumbnail.sorl.net/examples.html#template-examples
{% thumbnail item.image my_size_string crop="left" as im %}
<img src="{{ im.url }}">
{% empty %}
<p>No image</p>
{% endthumbnail %}
it works for me.
I am using couchdb-python with Django. I am looking for a way to display an image (which is stored in the database as an attachment to a document) in a template. Oddly, I cannot find any example online of how to do this.
Currently, in the views.py I have something like this:
def displaypage(request,id):
docs = SERVER['docs']
try:
doc = docs[id]
except ResourceNotFound:
raise Http404
...
attachments = doc['_attachments']['someimage.jpg']
...
text_marked_down = markdown.markdown(doc['text'])
return render_to_response('couch_docs/display.html',{'row':doc,'attachments':attachments,'doctext':text_marked_down,...},context_instance=RequestContext(request))
Then, in the template display.html:
{% extends 'site_base.html' %}
{% block wrapper %}
{{ attachments }}
<div>{{ doctext|safe }}</div>
{{ endblock }}
I am seeing the text just fine, but for the image I only see the following:
{u'stub':True, u'length':27018,u'revpos':19,u'content_type': u'image/jpeg'}
So, clearly I am not passing the actual image, or not displaying it correctly anyway. Oddly, I cannot find an example online anywhere of how to actually do this. Can anyone point me to one, or provide it here?
You are using the template engine to render an HTML document. That document will be interpreted by the web browser just like any other HTML document.
Think about how an HTML page contains an image. The image is never inline within the HTML document itself. The HTML page contains a reference to instruct the browser to separately load the image and display it in place.
<img src="/path/to/image" />
So, likewise, you will need to:
create a separate view that will only return the binary data of the image. Set the mime type appropriately. See http://effbot.org/zone/django-pil.htm for some ideas how to return an image, but in your case set the contents of the response to be your image content.
add an <img ...> tag to your template that calls the new view you created.
once you drill down your db, you might want to consider building the url of each documents attachment as follows:
def function():
couch = couchdb.Server() #connect to server
db = couch['img'] #connect to database which contains docs with img attachments
doc_id = [] #create list of id's
http_docid = [] #create list to populate href for picture path
for i in db: #for each id in the db
doc_id.append(i) #add to the carid list
doc = db[i] #get the document id
for key in (doc['_attachments']): #for the key in the doc '_attacments' payload
print key #just to confirm
href_docid.append(('http://yourdbDomain/dbname/'+i+'/'+key)) #create a uri and append to a list
return href_docid
And below im using Jinja2's templating:
{% for img in function() %}
<img class="some-class" src="{{ img }}">
{% endfor %}
Hope this proves usefull!