Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django listview中的API数据_Django_Django Views_Api Design - Fatal编程技术网

Django listview中的API数据

Django listview中的API数据,django,django-views,api-design,Django,Django Views,Api Design,在Django中,很难理解如何将API中的数据添加到listview中。我使用的是django restframework,我的一部分数据有一个内部API。最后,我希望将货运模型中的所有信息显示在列表视图中。我知道我必须创建一个上下文,但我不知道如何使用API数据来创建上下文。以下是我目前的代码: @method_decorator(login_required, name='dispatch') class ManifestEngineView(ListView): model = F

在Django中,很难理解如何将API中的数据添加到listview中。我使用的是django restframework,我的一部分数据有一个内部API。最后,我希望将货运模型中的所有信息显示在列表视图中。我知道我必须创建一个上下文,但我不知道如何使用API数据来创建上下文。以下是我目前的代码:

@method_decorator(login_required, name='dispatch')
class ManifestEngineView(ListView):
    model = Freight
    template_name = 'pages/engine.html'

    def get_context_data(self, **kwargs):
        freight_list = requests.get('http://localhost:8000/api/freight/').json()
        for freight in freight_list:
            print(freight['pu_customer'])
我可以将客户名称打印到控制台上,但我不知道如何将其置于可以传递到模板并使用的上下文中。模板应具有一个包含模型中所有必要数据的框

更新的模板代码:

<div id="origin" class="fbox">
            {% for f in freight.pu_customer %}
                <span class="freight draggable">
                {{ f }}
                </span>
            {% endfor %}
    </div>

{f在运费中的百分比。pu_客户%}
{{f}}
{%endfor%}
在模板中尝试此操作

{% for f in something %}
{{ f }}
{% endfor %}

API打算从模板中使用,使用Ajax和Jquery。 在您的情况下,您可以简单地使用ListView

in views.py

class ManifestEngineView(ListView):
    model = Freight
    template_name = 'pages/engine.html'
    context_object_name = 'list_freights' 
在模板中

 {% for item in list_freights %} //what you named your context
    <tr>
        <td>{{ item.pu_customer }}</td>
</tr>
{%for list_freights%%中的项目//您将上下文命名为什么
{{item.pu_customer}}
class ManifestEngineView(ListView):
    model = Freight
    template_name = 'pages/engine.html'
    context_object_name = 'list_freights' 
 {% for item in list_freights %} //what you named your context
    <tr>
        <td>{{ item.pu_customer }}</td>
</tr>