Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to send url from template to view in django

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 %}" class="effect-lily tm-post-link tm-pt-60">

Error I Get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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"),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading