Home > front end >  How to send url from template to view in django
How to send url from template to view in django

Time:02-25

How can I send a URL from a template to a view? I have noticed that a normal string works but a URL doesn't. Here is what I have tried.

path('imageviewer/<str:theimage>', mainviews.imageviewer, name="imageviewer"),
    
def imageviewer(request, theimage):
    response = render(request, "imageviewer.html", {"theimage": theimage})
    return response

How I attempt to pass it : (value.image) is a url

<a href="{% url 'imageviewer' theimage=value.image %}" >

Error I Get:

Reverse for 'imageviewer' with keyword arguments '{'theimage': 'https://storage.googleapis.com/katakata-cb1db.appspot.com/images/humours/1643758561'}' not found. 1 pattern(s) tried: ['imageviewer/(?P<theimage>[^/] )\\Z']

Thank you.

CodePudding user response:

You need to escape the image so that it can be used in a URL, use the built-in filter urlencode

    {% url 'imageviewer' theimage=value.image|urlencode %}

Your urlpattern also needs to accept slashes and other chars, use the path converter instead of str as it accepts any non-empty string

path('imageviewer/<path:theimage>', mainviews.imageviewer, name="imageviewer"),
  • Related