how to decode ldap3 thumbnailPhoto to display it in template? - django

I'm trying to load a picture from active directory into a django template.
This is the result :
"b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00\x00\x00\x00\x00\x00\xff\xdb\x00C\x00\x02\x01\x01\x01\x01\..."
I have seen the PHP method and tried to implement it using python with no success. I also tried the base64.decode, to save it in an image file and convert it using pil then load it, base64.decodebase64, I even tried to convert it using javascript and load it in src but all these methods didn't work, I have read a lot of articles but none helped me.
ANy help would be appreciated.

I know that it's old. But if someone struggles with the same problem here's how I did it:
import base64
...
thumbnailPhoto = base64.b64encode(entry.thumbnailPhoto.value).decode("utf-8")
And then in the html template
<img src="data:image/gif;base64,{{ thumbnailPhoto }}">

Related

Trying to get all image links of reddit.com using python and re

I've looked through other posts and have tried to implement what they have said into my code but I'm still missing something.
What I am trying to do is get all the image links off a website, specifically reddit.com
and once I obtain the links to display the images in my browser or download them and display them through Windows Image Viewer. I am just trying to practice and broaden my python skills.
I am stuck at obtaining the links and choosing how to display the images.
What I have right now is:
import urllib2
import re
links=urllib2.urlopen("http://www.reddit.com").read()
found=re.findall("http://imgur.com/+\w+.jpg", links)
print found #Just for testing purposes, to see what links are found
Thanks for the help.
The imgur.com links on reddit do not have any .jpg extensions, so your regular expression won't match anything. You should be looking for the i.imgur.com domain instead.
Matching re.findall("http://i.imgur.com/\w+.jpg", links) does return results:
>>> re.findall("http://i.imgur.com/\w+.jpg", links)
['http://i.imgur.com/PMNZ2.jpg', 'http://i.imgur.com/akg4l.jpg', 'http://i.imgur.com/dAHtq.jpg', 'http://i.imgur.com/dAHtq.jpg', 'http://i.imgur.com/nT73r.jpg', 'http://i.imgur.com/nT73r.jpg', 'http://i.imgur.com/z2wIl.jpg', 'http://i.imgur.com/z2wIl.jpg']
You can expand this to other file extensions:
>>> re.findall("http://i.imgur.com/\w+.(?:jpg|gif|png)", links)
['http://i.imgur.com/PMNZ2.jpg', 'http://i.imgur.com/akg4l.jpg', 'http://i.imgur.com/dAHtq.jpg', 'http://i.imgur.com/dAHtq.jpg', 'http://i.imgur.com/rsIfN.png', 'http://i.imgur.com/rsIfN.png', 'http://i.imgur.com/nT73r.jpg', 'http://i.imgur.com/nT73r.jpg', 'http://i.imgur.com/bPs5N.gif', 'http://i.imgur.com/z2wIl.jpg', 'http://i.imgur.com/z2wIl.jpg']
You may want to use a proper HTML parser instead of a regular expression; I can recommend both BeautifulSoup and lxml. It'll make it much easier to find all <img /> tags that use i.imgur.com links with those tools, including .gif and .png files, if any.

"UnicodeEncodeError",'charmap' codec can't encode characters In Django

I am working with Django.
I have a HTML form which collects information from the user,wrote a function which post the value.But when I amtrying to render the value to an URL.(Example:return of the function is like:http://api.test.com?search=topic?title=USA).
But it shows UnicodeEncodeError with
'charmap' codec can't encode characters
I googled and try to solve from other solution found.Such as using external libraries.
Actually,I want to add user parameter within URL that will be shown in a text area.
How can I do that?
You should use Urllib.urlencode to create get parameters to be added to the url.
I guess, that should solve your problem

Textile renders to HTML, but Django is escaping it

I have a bunch of posts stored in text files formatted in yaml/textile (from Jekyll) that I am trying to import into my new Django project. The problem is that Django is escaping the actual html code, meaning my post is not getting formatted. How can I got about fixing this? should I change something in the jekyll-import command (a custom manage.py command), the postgresql server, or the views.py file?
Example:
The one thing I can’t do is write about myself. Hell, look at my about me page.
Well, I figured it out right after I posted it. I had tried this before but used the wrong syntax. To do this I just had to add '|safe' to the end of my body tag.
Like so:
{{ body|safe }}
Very nice.

Why is my Django app not displaying dynamic images when the path is correct?

My Django 1.1 app uses dynamic images.
I'm confused about why the path generated from my template tag:
{{image_product.photo.path}}
looks correct, but does not display the requested image.
This generates a path that works:
src='/media/{{image_product.photo}}' => <img src='/media/lcdtvs/product1.jpg'>
This DOES NOT work:
src='{{image_product.photo.path}}' => <img src='/Users/Bryan/work/review_app/media/lcdtvs/product1.jpg'>
I checked to confirm that the Absolute path generated from MEDIA_ROOT is correct on my computer and it works fine.
Why would the image not display correctly?
Two things to keep in mind:
What you want is the {{image_field.url}} method (not the path).
If it still is 404, either you need to setup your server correctly, or if you are using the development server you need to enable it to server static files.
Not familiar with Django, but I'd guess you need to have file:///Users/... in the second snippet.

Django admin file upload doesn't work, how do I debug this?

I'm on Mac Leopard. Trying to get django admin file uploads, or really image uploads to work. In my app's models.py I set the field to:
image = models.FileField(upload_to='images', max_length=500)
(started w/ ImageField, but thought if a file doesn't work, then the image for sure won't work)
It says the upload happened. Gives me a positive result. Saves the path to my database. But looking for the file, well, it isn't there.
Since there's no error message, it's hard to debug.
Tried various permissions, but nothing is working.
Any ideas?
Is your MEDIA_ROOT set correctly? http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
From the comments it sounds like you found a solution to your issue. For future reference, you may want to check out django-debug-toolbar to help you troubleshoot problems like this (Especially when there's no error, it just doesn't work right!). Code available here: http://github.com/robhudson/django-debug-toolbar/tree/master