0

I'm a Django beginner, how do I get the ID of each image uploaded with form?

class Image(models.Model):
    imageuploader_profile=models.ForeignKey(settings.AUTH_USER_MODEL)
    upload_image=models.ImageField() 

def upload(request):
    if request.method == 'POST':
        form=UploadForm(request.POST, request.FILES)
        if form.is_valid():
            post=form.save(commit=False)
            post.imageuploader_profile=request.user
            post.save()
            return redirect.... 
    else:
        form=UploadForm
    return render...... 

view.py

def home(request) :
    all_images=Image objects.filter(imageuploader_profile=request.user) 
    context={'all_images':all_images} 
    return render(request, 'home.html', context) 

My home.html

{% for post in all_images %}
{% if post upload_images %}
<img src="{{ post upload_image.url }}"> 
{% endif %}
{% endfor%} 

I want to get all IDs in my template.

zypro
  • 1,158
  • 3
  • 12
  • 33
GistDom Blog
  • 51
  • 1
  • 8
  • 1
    So you want something like `Image.objects.filter(uploaded_via='form')`? Then you need to store the information who the image got saved/created. If you just want to get all Images (because you only save them by form) then you can do `images = Image.objects.all()`. – zypro Mar 27 '20 at 12:26
  • @zypro... Can you update your answer with the form in my question. I seems confused on how I can get the ID of each image i uploaded with form. Where do you get 'uploaded_via' from? – GistDom Blog Mar 27 '20 at 12:29
  • The question rather is, where do you need all images? What do you want to do with this queryset? Then I can update your code. Or do you only need the current uploaded image ID? – zypro Mar 27 '20 at 12:31
  • @zypro... I want to upload an image that will be displayed in homepage, I was able to do that. But what I want now is to get the ID of each image i upload using form. – GistDom Blog Mar 27 '20 at 12:33
  • You are still unclear, please tell exactly what required output you want, for e.g. (I want list of every image id that users have uploaded in my template) – Hisham___Pak Mar 27 '20 at 13:01

3 Answers3

1

You can write a new method/view to get all IDs:

def get_images_ids():
    images_ids = Image.objects.values_list('id)
    print(images_ids)
    return images_ids

Here the explanation for values_list (you also can use values() if you need more, or the attribute flat=True).

If you call get_images_ids() you get all IDs of all images in a queryset (or in a list with flat=True). If you need to filter them use .filter().

In your views.py

def home(request) :
    all_images=Image objects.filter(imageuploader_profile=request.user) 
    context={'all_images':all_images, all_images_ids: get_images_ids()} 
    return render(request, 'home.html', context) 

In your template do this:

{% for post in all_images %}
    {% if post upload_images %}
        <img src="{{ post.upload_image.url }}">
        <span>{{ post.id }}</span>
    {% endif %}
{% endfor%}

or

<ul>
{% for id in all_images_ids %}
    <li>{{ id }}</li>
{% endfor%}
</ul>
zypro
  • 1,158
  • 3
  • 12
  • 33
  • How do I print out the ID, so I can see in console? And also I do I pass this in template. I will update my question so that it can be clear to you. – GistDom Blog Mar 27 '20 at 12:45
  • call `get_images_ids()` and print its output. `print(get_images_ids())`. To pass it into the template you can define it as an template tag, or add it the the desired view as an context value. – zypro Mar 27 '20 at 12:47
  • 1
    Any how and where do you want to print the id? – zypro Mar 27 '20 at 12:58
  • I want to print out the ID in console, so I can see myself when the image is uploaded. And how do I pass get_images_ids in template? Edit my template code in my question so I can understand. – GistDom Blog Mar 27 '20 at 13:01
  • @GistDomBlog Just asking, what is the purpose of passing `get_images_ids()` into template? `post.id approach is much better you will not be required even to create that function. – Hisham___Pak Mar 27 '20 at 13:10
  • @zypro.. Thanks, I have just one one question to ask concerning how I can implement comment to each image i uploaded. I will update my question with comment model. – GistDom Blog Mar 27 '20 at 13:18
  • @zypro...i can not edit my question anymore. Click on this link. https://stackoverflow.com/questions/60881660/how-to-create-comment-form – GistDom Blog Mar 27 '20 at 13:24
  • @zypro... Please kindly take a look at the link. – GistDom Blog Mar 27 '20 at 15:09
1

You can't get id of the image unless you first create an object in database, in other words you will have to execute post.save() first and then you can run post.id to get id. Here is an example with your code.

class Image(models.Model):
    imageuploader_profile=models.ForeignKey(settings.AUTH_USER_MODEL)
    upload_image=models.ImageField() 

def upload(request):
    if request.method == 'POST':
        form=UploadForm(request.POST, request.FILES)
        if form.is_valid():
            post=form.save(commit=False)
            post.imageuploader_profile=request.user
            post.save()
            print(post.id)  # This will print id of submitted post in your console 

            return redirect.... 
    else:
        form=UploadForm

Note: ID of post is automatically created in the database after you run post.save()

Hisham___Pak
  • 1,472
  • 1
  • 11
  • 23
0

You can get the ID of the image after post.save() executes.

So before the return statement you can access post.id or post.pk to get the image ID.

UPDATE:

class Image(models.Model):
    imageuploader_profile=models.ForeignKey(settings.AUTH_USER_MODEL)
    upload_image=models.ImageField() 

def upload(request):
    if request.method == 'POST':
        form=UploadForm(request.POST, request.FILES)
        if form.is_valid():
            post=form.save(commit=False)
            post.imageuploader_profile=request.user
            post.save()

            # here you can access post.id or post.pk

            return redirect.... 
    else:
        form=UploadForm

If you want to get all uploaded images id then use Image.objects.values_list('id)

Nahidur Rahman
  • 795
  • 1
  • 6
  • 17