Livetuts Bj
Livetuts Bj

Reputation: 131

Django, call the same view function with and without parameter

I want to pass the image ID to the same view function and pass the Image to the template for display.

In my analysis app urls.py I have used

path('', views.image_analysis, name='home'),
path('<int:id>/', views.image_analysis, name='image_display'),

The view function is

What should I do there to get the id in the function. If I do like def image_analysis(request, id) its giving error.

def image_analysis(request):
    if request.method == 'GET':
        post_id = Image_upload.objects.get(id=id)

    all_images = Image_upload.objects.filter(user=request.user)

    return render(request,
                'analysis/home.html',
                {'images': all_images}
                )

In the home.html template file.

{% block content %}

<div class="row">
    <div class="col-sm-4">
        <h2>Images</h2>
        <ul class="list-group">
        {% for image in images %}

            <li class="list-group-item"><a href="{% url 'analysis:image_display' image.id %}">{{image.image_name}} -</a> {{image.status}}</li>



        {% endfor %}
        </ul>
    </div>
    <div class="col-sm-8">  Single Image will come here.</div>
  </div>

The template HTML file is having 2 blocks the left one is with all the images from the user on clicking I want to display the image in the same place.

Can you please help me.

Upvotes: 1

Views: 2662

Answers (2)

Arun T
Arun T

Reputation: 1610

You can make your view function as the following:

def image_analysis(request, id=None):
    if request.method == 'GET':
        try:
            post_id = Image_upload.objects.get(id=id)
        except:
            <do the logic that you need if id doesnt exist in url>

    all_images = Image_upload.objects.filter(user=request.user)

    return render(request,
                'analysis/home.html',
                {'images': all_images}
                )

If you are giving id in the function definition, then it will always look for it to be given, since it is optional for you, we put it as id=None.

Inside the function you are performing a query with that id, but obviously there wont be any id = None in the DB table. hence you need to catch that exception and do the logic for what happens if there is no id in url inside that except block.

Upvotes: 3

Mehak
Mehak

Reputation: 961

You can get the id in the image_analysis function by using kwargs.

Change your function like so:

def image_analysis(request, *args, **kwargs):
    id = kwargs.get('id')
    if request.method == 'GET' and id:
        post_id = Image_upload.objects.get(id=id)
    else:
        post_id = None # Replace with relevant code

    all_images = Image_upload.objects.filter(user=request.user)

    return render(request,
                'analysis/home.html',
                {'images': all_images}
                )

In case after this change, you get a matching query does not exist error, try swapping the URLs.

path('<int:id>/', views.image_analysis, name='image_display'),
path('', views.image_analysis, name='home'),

Upvotes: 2

Related Questions