I am trying to create a picture slideshow which will show all the png and jpg files of a folder using django.
Problem is how do I open windows explorer through django and prompt user to choose a folder name to load images from. Once this is done, how do I read all image files from this folder? Can I store all image files from this folder inside a list and pass this list in template views through context?
This link “https://github.com/csev/dj4e-samples/tree/master/pics”
shows how to store data into to database(sqlite is the database used here) using Django forms. But you cannot upload an entire folder at once, so you have to create a one to many model between display_id(This is just a field name in models you can name it anything you want) and pics. Now you can individually upload all pics in the folder to the same display _id and access all of them using this display_id. Also make sure to pass content_type for jpg and png separately while retrieving the pics.
Related
I am using Django 2.2 and Pillow library in the models fields to handle the image files.
I studied a latest online course on Django where tutor used special method during image save to make sure that similarly named images will not override .
But I noticed that when a user upload two different images with same name , Django renames the second file without any function and the files never conflicted when we retrieve.
Is this a new feature in Django ? Should I follow file renaming convention ?
My problem is that I want to store images in database, but I don't want to store them physically in my project as static files, I want to save in database url to images which are already uploaded somewhere in Internet. How can I do this, just use CharField or TextField as type of field and paste url? But I also want later to display this photo in my template.
class Image(models.Model):
name = models.CharField(max_length=25, verbose_name='Image name')
image_file =
def __str__(self):
return self.name
If you want to use the external image source link in you django project, all you need is storing image source link in you model which you already did this in you models.py:
name = models.CharField(max_length=25, verbose_name='Image name')
But if consider this that you can't use google images source's link because of some limit, read this article to understand why :
What Happened To Google Image Search And Why You Can No Longer View Images Directly
by the way you can use urlfiled instead of using charfiled, because it's only store urls. Django urlfield
after storing your image source link in your model, you can get it in you view the pass it too template and load the image in your template without downloading your image.
if you need any further help, ask and will happy to help.
As a newbie to django, I'm having an insanely difficult time figuring out how to upload a folder to the site directory with django. Let me give some context. I'm making a sort of blog. In this blog, I store details on an article in the database, such as headline, author, etc... Then, I write the article in word, save it as an html document, and upload the file to django. All of this can be done through the builtin admin page. But the reason I need to upload an entire folder is because if I use images in my article, Word places them inside a folder with the same name as the article and appends '.fld' to the folder name. Let's say I save an article as 'test.html'. It saves that file and any other files it needs in a folder named 'test.fld' right next to the html file. If I could upload the article and folder along with it, it would make life a lot easier. That being said, the django documentation for uploading multiple files sucks. I was wondering if anyone could give me clearer explanation on how to do this. And if possible, how to upload an entire folder.
I am impressed with how wordpress handle the images (where user can upload and insert images into post). I want to make this feature to able applied in django. How do I achieve it?
Basically I have two models :
1.post - for writing the content
2.media - for uploading images/files
But in the post model I would like to able insert the images from the media model in the textField(). For the textField I am using django-tinymce for rich text context with filebrowser integration. So, better use the Image button to upload/insert image to/from media model.
Also the user only allow to browse the images that he/she uploaded in the filebrowser.
Lastly beside in the admin, this feature also need to work in frontend.
So is this possible?
I'm creating a basic blog in Django. One of my classes is for the authors. Each contains Name, Position, Biography, etc., and I'm attempting to associate existing images with each author to be used as avatars, essentially.
FileField and ImageField aren't what I want to use as I already have images stored statically elsewhere- I just want to specify each image's path manually in the Django admin as a URL (CharField/TextField, whatever) and have that image served when I call {{ author.photo }} in a template, for example. I also don't necessarily want to source the image path from the class model, as the structure and paths of the existing images may change. Any ideas?
Well you have to save it somewhere in order to map the user and the image....
So I can think of 2 solutions -
Have a field that stores the path. Then you can access it via the template, something like this - <img src="{{author.photo_path}}">
If you're saying the path might change, you can have a user ID for each user, and store his photo with that ID. Then you can simply do <img src="/path/{{author.id}}_picture.gif">
Using the second solution, you can also have the path as a variable in your code, and then pass it to the template, and access the picture like this -
<img src="/{{path}}/{{author.id}}_picture.gif">, which doesn't force you to have a hardcoded path, and once the path changes you only change it once, not in all your model instances/template files