简体   繁体   English

Django:在Java脚本中引用视图列表?

[英]Django: Referencing Views List in Java Script?

So I'm working with Django on a website that I am playing around with, and have been trying to research how I could get the following list in my views.py and be able to reference it in my javascript? 因此,我正在与我一起玩的网站上使用Django,并且一直在尝试研究如何在我的views.py中获得以下列表并能够在我的javascript中引用它? I'm working on creating an ajax call and the tutorials I am coming accross are a bit confusing. 我正在创建一个ajax调用,而我正在学习的教程有些令人困惑。

#lines 6 - 8 of my code.

def catalog_home(request):
    item_list = item.objects.order_by('name')   #item is the model name

note: the item model containts a name, description, overview and icon column. 注意:项目模型包含名称,描述,概述和图标列。

Is it possible for me to use the list above (item_list) and be able to write a javascript function that does something similar to this? 我是否可以使用上面的列表(item_list)并能够编写执行类似于此操作的javascript函数? :

$(document).ready(function() {
    $("#showmorebutton").click(function() {
    $("table").append("<tr></tr>");
    for (var i = 0; i < 3; i++) {
            var itemdescription = item.description;
            var itemName = item.name;
            var icon = item.icon;
            $("table tr:last").append(generateCard(itemName,
                                                itemdescription,
                                                icon));
    }


function generateCard(itemNameC, itemdescriptionC, iconC) {
    var card = "<td class='tablecells'><a class='tabletext' href='#'><span class='fa "
        + iconC
        + " concepticons'></span><h2 class='header'>"
        + itemNameC
        + "</h2><p>"
        + itemdescripionC
        + "<span class='fa fa-chevron-circle-right'></span></p></a></td>";
return card;
}

I don't mean to crowd source the answer to this, I just would appreciate any feedback/advice for me to handle this task, as I am fairly new to coding. 我并不是要众说纷answer,我只是想感谢我为处理此任务提供的任何反馈/建议,因为我是编码的新手。

You should absolutely be able to do this. 您应该绝对能够做到这一点。 The trick is to understand Django templates. 诀窍是了解Django模板。 You showed part of the view but you will need to render to a template. 您显示了视图的一部分,但需要渲染到模板。 Inside the template, you can just do something like 在模板内部,您可以执行以下操作

HTML code for page goes here (if you're mixing js and html)
<script>
  var items = [{% for d in data %}
{% if forloop.last %}
    {{ d }}
{% else %}
    {{ d }},
{% endif %}
{% endfor %}];
  // code that uses items
</script>

Note there's a little bit of work required to make sure you have the right # of commas [taken from this SO answer and your code should handle the case where the array is empty. 请注意,需要做一些工作来确保您具有正确的逗号数[从此SO答案中获取,并且您的代码应处理数组为空的情况。

Alternatively, you could do an ajax call from static javascript to your django server using something like django rest framework to get the list of items as a json object. 另外,您可以使用django rest框架之类的工具从静态javascript到django服务器进行ajax调用,以将项目列表作为json对象获取。

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

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