本文介绍了带有标签或href的Django数据传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含链接的表,当单击链接以执行更多操作时,我想将一些数据传递给我的函数.
I have a table that contains a link and I would like to pass some data to my a function when the link is clicked for more actions.
***my.html***
<table>
<tbody>
{% for query in queries %}
<tr>
<td name="my_data" value="{{query.name}}"><a href="/internal/my_func">{{query.name}}</a>
</tr>
{% endfor %}
</tbody>
</table>
这是我的网址
***urls.py***
from django.conf.urls import url, patterns
from app.views import *
urlpatterns = patterns('',
url(r'^internal/my_func$', my_func),
)
我的观点
***views.py***
def my_func(request):
# how do i get the query.name value here for me to perform more actions on it
query = request.Get.get('my_data')
# this does not work
推荐答案
我会尝试在url字符串中为锚标记使用查询参数:
I would try using query parameters in the url string for your anchor tag:
<a href="/internal/my_func?query_name={{ query.name|urlencode }}">{{ query.name }}</a>
然后在您的视图文件中,可以使用以下命令进行访问:
Then in your view file you can access it with:
def my_func(request):
# Get query_name from request
query = request.GET.get('query_name')
请注意,我使用的是 request.GET
而不是 request.Get
.
Note that I used request.GET
instead of request.Get
.
这篇关于带有标签或href的Django数据传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!