How to pass a variable to views from a template using href ?

26 views
Skip to first unread message

ratnadeep ray

unread,
May 19, 2020, 9:14:19 AM5/19/20
to Django users
Hi all, 

My requirement is to pass a variable to the view from the template using href. 

For example, I have written the following line of code: 

<a href="/execute" value="SQL">SQL data</a>

So using the above line, can we send the value "SQL" clicking on the link of "SQL data" to the method "execute" written in the views? If not, then what way can I do so? 

Thanks. 





Vishesh Mangla

unread,
May 19, 2020, 9:24:45 AM5/19/20
to django...@googlegroups.com

In your views.py check “dir” on request.GET object to see what you have got.

 

Sent from Mail for Windows 10

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7e892826-f4c1-426d-bf9b-1a1b0b2b17be%40googlegroups.com.

 

Desh Deepak

unread,
May 19, 2020, 9:57:32 AM5/19/20
to django...@googlegroups.com
You can use slug

Ignat Petrov

unread,
May 19, 2020, 1:44:02 PM5/19/20
to Django users
You should be able to do that by using a relative path instead of the absolute "/execute". Just use something like href="{% url 'execute' 'SQL' %}. For this to work you need to name the url path as 'execute'. That way you'll pass the 'SQL' value to the endpoint.
Here is example from the polls tutorial: https://docs.djangoproject.com/en/3.0/intro/tutorial03/
Reply all
Reply to author
Forward
0 new messages