I'm installed Flask-images for resizing some images. My code are like this:
<img src = '{{url_for('showimages', filename = market.thumbnail, width=100, height=100, mode='crop')}}'>
showimages:
#app.route('/image/user/<filename>')
def showthumbnail(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
There are nothing happens and my Chrome devaloper tools said image's url like this:
<img src="/image/user/Untitled-1.png?width=100&height=100&mode=crop">
I know there is another way instead url_for - resized_img_src().
I'm set IMAGES_PATH= os.path.join(APP_ROOT, 'images/').
However this configuration does not work and when I use resized_img_src(), only got broken image icon. I don't have a clue how can fix this.
+Is there any other easy ways to resizing uploaded images?
Your template uses showimages whereas your Flask Python code shows showthumnails, I'll assume this is typo and code actually uses same as template.
Are these really the quote characters you are using in your template? You need to use the single and double quotes or escape the inner single ones. Try this instead:
<img src="{{url_for('showimages', filename=market.thumbnail, width=100, height=100, mode='crop')}}">
Related
I am creating a program that generates a chart, and then displays the chart on the webpage. I want each chart generated to have a unique filename. However, once that unique filename is generated, I don't know how to refer to it within the html file.
I use this to create a random filename starting with "chart" in the "images" folder. This parts works fine.
basename = "images/chart"
suffix = str(uuid.uuid4())
filename = "_".join([basename, suffix])
plt.savefig(filename)
I then have this in the html file, but don't know how to modify to add the random suffix part of the name that was just generated.
<img src="{{ url_for('static', filename = '/images/chart.png') }}">
It is hard to say without knowing more about the structure of your application, but one approach might be to change the template to something like
<img src="{{url}}">
and then calling it with
render_template('template.html', url=url_for('static', filename))
I'm no flask expert, but my instinct would be to avoid putting too much Python code — things like the url_for call — in the templates, because I do not want to mix program logic with presentation.
I'm trying to make app like gist.github.com. After saving code snippets they looks in like long string. I tried different filters like "safe, escape ... etc". Nothing helped me.
In database code looks like:
def asd(a):
return a+2
asd(2)
This is my template code:
<div>{{ s.code|escape }}</div>
Result is:
def asd(a): return a+2 asd(2)
This doesn't have anything to do with escaping or marking as safe. In fact, it doesn't have anything to do with Django at all.
HTML ignores whitespace, including newlines. If you want to show your code as formatted in the db you should use the <pre> tag.
Whenever I try to image.url function I get an empty string. I also tried the image.path but didn't help. I am using Django 2.15. and I have a Image field in models. My {{MEDIA_URL}} returns /media.
I have done Django the config for setting media root, and media url in settings. py also URL pattern.
When I go to the link foo.com/media/imagefile.png it loads perfectly fine. But when I pass through views functions a dictionary
render (request ,templatepath,object.__dict__)
and in template type
{{img.url}}
I get a empty string.
where as I want to see the real image, or its url mentioned.
Any help is appreciated.
This is what I have in urls.py
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I figured out the solution, The problem was caused because I passed the template a regular dictionary. Dictionary object with key img has just image name stored as a value, thus it has no url function to return. In order to solve this problem I just passed the whole query dictionary which had python objects and necessary function and variables like '.url' in the image field.
example
objectpassed = Class.object.all()
render(request,templatepath,{ 'somename':objectpassed}
To give you an idea of my starting point:
I have a set of forms with a label and checkbox.
Some of my labels are supposed to display pictures, but not all.
The pictures when they should be displayed are never at the same place with regards to the label text (ie sometimes the picture should be displayed in the middle of the label, sometimes at the beginning, end...).
So the only way I could think of to address this problem was to have in my label at some place, something like
<img src="{{ MEDIA_URL }}/sources/1/2.jpg" height=300px/>
within the text.
The problem is that it is considered as a string and not as an html tag.
Would you have any idea of to address this issue? I'm currently run out of ideas.
Thank you very much.
Soso :)
I managed to make it work. The only thing I needed was to wrap my label containing my former <img> tag with:
mark_safe
imported using
from django.utils.safestring import mark_safe
Thanks!
I have a situation where I need to differentiate two calls by the path in the source of a HTML. This is how the img tag looks like
<img src="/folder/12280218/160024536.images.jpg" />
I am planning to alter the source to
<img src="/folder/12280218/160024536.images.jpg/1" />
observe the "/1" at the end of src
I need this so that I can change the flow in the controller when I am serving this image.
This is what I have tried until now.
my $string = '<p><img src="/folder/12280218/160024536.images.jpg" /></p>';
$string =~ s/<img\s+src\=\"(.*)"\s+\/><\/p>/<img src\=\"$1\/1" \><\/p>/g;
This is working as long as the $string looks like this.
In our application, user has the ability to alter the HTML input using CKEditor.
He can alter the image tag by adding width="800" before or after the src attribute. I want the regular expression to handle all these situations.
Please let me know how to proceed.
Thanks in advance.
Replace :
(<img.*src="[^"]*)(".*\/>)
by
$1/1$2
Demo here
Edit : Changed the regex to handle situations with other attributes (like the "width" part)