简体   繁体   English

如何在Django中将字符串从HTML文件获取/传递到Views文件?

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

I've some strings/text in an HTML file. 我在HTML文件中有一些字符串/文本。 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? 如何从HTML将文本/字符串获取到Django视图,以便可以使用它们?

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. first_key是zip文件,因此我浏览了两个列表项广告,将这些列表项作为字符串/文本来打印在我的一个页面上。 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}}”。 I've tried request.POST.get['mytext'] but all I'm getting is NONE. 我已经尝试过request.POST.get ['mytext'],但是我得到的只是NONE。 I want '{{i}}' stored in a variable in my views.py file. 我希望将“ {{i}}”存储在views.py文件的变量中。

{% 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. @dirkgroten帮助我找到了它。

First of all I passed the value from href in anchor tag itself through URL something like "{% url 'my_app:page' key=i %}" 首先,我通过URL传递了锚标签本身中href中的值,例如"{% 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+]+ 然后在我的urls.py文件中,将路径从page_view/更改为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. 最后,在我的views.py文件中,我将键值作为参数传递给函数,例如def page(request, key):然后我可以在其中一个视图中使用key的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串从 HTML 文件传递​​到 django 中查看? - How can I pass string from HTML file to view in django? 如何将值/参数从HTML传递到Django Views? - How can I pass values/parameters from HTML to Django Views? 如何将值从html文件传递到views.py文件? - How to pass value from html file to views.py file? 如何从两个视图Django传递相同的上下文? - How can I pass the same context from two views, Django? Django:如何从views.py获取字典到javascript函数(html文件) - Django: how to get a dictionary from views.py into javascript function (html file) Python Django如何无法从HTML文件上的views.py呈现我的变量 - Python Django how can't render my variables from views.py on html file Django:将带有 HTML 和 Django 模板标签的 Views.py 中的字符串变量注入到 HTML 文件中 - Django: Inject a string variable from Views.py with HTML and Django template tags into HTML file 如何从文件views.py的function将图像地址返回到Django模板? - How can I return image address to Django template from a function of the file views.py? 如何从 views.py 文件中导入 django 的 ChoiceField 中的所有选项? - how can i import all choices in a ChoiceField in django from the views.py file? 如何将在我的html文件上单击的图像的URL传递给views.py中的python函数 - how to pass the url of image clicked on my html file to my python function in views.py Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM