Skip to content
Advertisement

Passing an html element from anchor tag to django view

I would like to know how i can pass the html link element as a string into a django view and have the information rendered on that specific view. to be clear: I have this

In html

 {% if list_1 %}
     <h3 class="display-5"><u>{{ choice }}</u></h3>
          <div class="row justify-content-md-center row-eq-height">
            {% for item in list_1 %}
            <div class="col-md-4">
                <a id = "search_item" name = 'search_item' href="{% url 'search_stock' %}" value={{ item }} target="_blank">{{item}}</a>

A django view rendered the items from a list and each item would ideally be clickable and the specific item would be passed into this view:

def search_stock(request):
   search_item = request.POST.get('search_item')
   ticker_request = search_item
.....

my urls.py reads as :

path('search_stock', views.search_stock, name='search_stock'),

The problem I think may be that the click action is not actually passing the value into the function based view and I am not sure how to work around this easily. Essentially once the information loads (each item is a link for a specific ticker), I would like the user to be able to click on that link with the ticker being passed into the search_stock function as a string in order to actually render the specific search_stock view.

Much thanks now and in advance.

Advertisement

Answer

try this,

in urls:

path('search_stock/<str:item>/', views.search_stock, name='search_stock'),

in views:

def search_stock(request, item):
   ticker_request = item

in html:

<a id="search_item" name="search_item" href="{% url 'search_stock' item %}" value={{item}} target="_blank">{{item}}</a>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement