简体   繁体   中英

How can I get/pass the string from HTML file to Views file in Django?

I've some strings/text in an HTML file. I want a new page when I click on them and individual string to give different view. How can I get the text/string from HTML to a Django view so that I can use them?

first_key is zip file so I'm lopping through two list items ad getting those list items as strings/text to print on one of my page. Upon clicking them I want a new page which I'm getting but unable to get the string/text/item value on which I clicked. I want the value '{{i}}' in my views. I've tried request.POST.get['mytext'] but all I'm getting is NONE. I want '{{i}}' stored in a variable in my views.py file.

{% for i,j in first_key %}
<tr>    
<td><a name="my_text" href = "{% url 'search_app:page'%}" >{{i}}</a></td>
<td>{{ j }}</td>
</tr>
{% endfor %}

根据您的代码从i = request.POST.get['my_text']获取i的值

 <td><a name="my_text" href = "{% url 'search_app:page'%}" >{{i}}</a></td>

Well... I found the answer and I'm posting it for others. @dirkgroten helped me in finding it out.

First of all I passed the value from href in anchor tag itself through URL something like "{% url 'my_app:page' key=i %}"

then in my urls.py file I changed the path from page_view/ to page_view/?P<key>[\\w+]+

and lastly in my views.py file I pass the key-value as an argument in the function something like def page(request, key): and then I was able to use the value of key in one of my views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM