GistDom Blog
GistDom Blog

Reputation: 51

How do I get the ID of an image

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.

Upvotes: 0

Views: 3415

Answers (3)

zypro
zypro

Reputation: 1183

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>

Upvotes: 1

Nahidur Rahman
Nahidur Rahman

Reputation: 815

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)

Upvotes: 0

Hisham___Pak
Hisham___Pak

Reputation: 1490

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()

Upvotes: 1

Related Questions