在django传递的数据中获取变量 [英] Get variables in datables passed by django

查看:160
本文介绍了在django传递的数据中获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的django应用程序中使用jquery datatables。我的表中的每一行都有一个命名的url到另一个页面与对象。
{%url'obj_details'obj.id%} 。当我点击url我得到没有反向匹配发现错误。我已经详细检查了这个问题。 url obj_details是存在于urls.py

  url(r'^ obj /(?P< pk> ; [\w  - ] +)/ details $',objDetailsView.as_view(),name ='obj_details')

datatables使用另一个视图来获取所需的数据,如下所示。

  def objdtable(request):

obj_json_tuple = list(obj.objects.all()。values_list(objnum,ob_field,date,price,field2,seller,id))

返回HttpResponse(json.dumps(obj_json_tuple))

我正在显示所有字段,除了ID在数据表中没有问题。

 列:[
{:fields.objnum },
{:fields.ob_field},
{:fields.date},
{:fields.price},
{:fields.field2},
{:fields.seller},
]

如何将objdtable视图返回的id作为url obj_details的参数传递?我试过 {%url'obj_details'fields.id%} {%url'obj_details'id%} 但是没有发现反向匹配发现错误。然后我在数据表中尝试

 data:
{
'bid':fields.id
}

并将命名的URL更改为链接 obj / data / details 我也尝试过将数据更改为fields.id,但没有任何效果。如何访问django在数据表中传递的变量?我可以在url中使用该变量,以便解决问题。



PS:将URL更改为 url(r'^ / obj / (?P< pk> \d +)/ details $,objDetailsView.as_view(),name ='obj_details')但是它给了我下面没有反向匹配发现的错误。 >

  NoReverseMatch at / myapp / obj / list / Reverse for'obj_details'with arguments'('',)'和keyword arguments'{} ' 未找到。 1模式尝试:['myapp / obj /(?P< pk> \\d +)/ details $'] 

html模板中的错误在下面粘贴。

 defaultContent: < a class ='btn btn-success btn-sm'href ={%url'obj_details'id%}style ='color:white'>< i class ='fa fa-eye fa-lg' >< / i>< / a> 


解决方案

您的问题是您正在尝试使用 jQuery 变量来呈现 Django URL。



对象JSON数据是 jQuery ,所以您不能在 {%url'obj_details'id%} 调用中使用它。



当我遇到这样的问题时,我会这样做:

 < script> 

//让我们假设你有你的jQuery ID值
var id = yourdict.obj.id;
//现在你有一个jQuery变量中的ID可以这样做
//生成一个带有aux ID'000'的URL
var URL ={%url'obj_details'000 %}
URL = URL.replace('000',id)//将aux ID替换为真实的

< / script>


I am using jquery datatables in my django app. every row in my table have a named url to another page together with the object. {% url 'obj_details' obj.id %}. When I click on the url I am getting no reverse match found error. I have inspected the issue in detail. The url obj_details is a named url that exists in the urls.py

url(r'^obj/(?P<pk>[\w-]+)/details$', objDetailsView.as_view(), name='obj_details')

datatables uses another view to get the required data it is as pasted below

def objdtable(request):

obj_json_tuple = list(Obj.objects.all().values_list("objnum", "ob_field", "date", "price", "field2", "seller", "id"))

return HttpResponse(json.dumps(obj_json_tuple))

I am displaying all fields except id in datatables without issues.

"columns": [
                   { "": "fields.objnum"},
                   { "": "fields.ob_field" },
                   { "": "fields.date" },
                   { "": "fields.price" },
                   { "": "fields.field2" },
                   { "": "fields.seller" },
           ]

How can I pass id returned by objdtable view as an argument to url obj_details? I have tried {% url 'obj_details' fields.id %} and {% url 'obj_details' id %} but bothe are giving no reverse match found errors. Then I tried in datatables

"data":
         {
         'bid':fields.id
         }

and changed the named url to the link obj/data/details I have also tried changing data to fields.id but nothing worked. How can I access a variable passed by django in datatables? I can use that variable in url so that the issue could be solved.

PS:On changing url to url(r'^/obj/(?P<pk>\d+)/details$, objDetailsView.as_view(), name='obj_details') but its giving me the no reverse match found error as pasted below.

NoReverseMatch at /myapp/obj/list/Reverse for 'obj_details' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['myapp/obj/(?P<pk>\\d+)/details$']

The error in html template is in the line pasted below.

"defaultContent": "<a class='btn btn-success btn-sm' href="{% url 'obj_details' id  %} " style='color:white'> <i class='fa fa-eye fa-lg'></i> </a>"

解决方案

Your issue is that you're trying to use a jQuery variable to render a Django URL.

The Object JSON data is jQuery, so you can't use it on your {% url 'obj_details' id %} call.

When I faced an issue like that I do something like this:

<script>

    // Let's supose you have your jQuery ID value here
    var id = yourdict.obj.id;
    // Now you have the ID inside a jQuery variable you can do this
    // Generate an URL with an aux ID '000'
    var URL = "{% url 'obj_details'  000  %}"
    URL = URL.replace('000', id)  // Replace the aux ID with the real one

</script>

这篇关于在django传递的数据中获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆