Django 如何使用从ajax发送的请求从views.py呈现表对象

Django 如何使用从ajax发送的请求从views.py呈现表对象,django,Django,我可以将我的对象数组从views.py发送到ajax,从ajax发送到html,这样我就可以访问html页面中的表数据了 HTML 如何从views.py获取对象。使用此处的模板在my.html页面中以表格格式显示 } views.py 您可以做的是使表格html成为自己的模板。然后,您可以在返回render时将其包含在其他模板中,还可以使用render_to_字符串将其包含在json响应中。这允许您在一个位置对表模板进行更改,但在使用表模板的任何地方都进行更改,而不是复制逻辑 table.ht

我可以将我的对象数组从views.py发送到ajax,从ajax发送到html,这样我就可以访问html页面中的表数据了

HTML

如何从views.py获取对象。使用此处的模板在my.html页面中以表格格式显示

}

views.py


您可以做的是使表格html成为自己的模板。然后,您可以在返回render时将其包含在其他模板中,还可以使用render_to_字符串将其包含在json响应中。这允许您在一个位置对表模板进行更改,但在使用表模板的任何地方都进行更改,而不是复制逻辑

table.html

<table>
{% for job in job_details %}
    <tr>
        <td>{{job.id}}</td>
        <td>{{job.user_id}}</td>
        <td>{{job.job_name}}</td>
        <td>{{job.start_time}}</td>
        <td>{{job.end_time}}</td>
        <td>{{job.job_url}}</td>
        <td>{{job.creation_date}}</td>
        <td>{{job.status}}</td>
    </tr>
{% endfor %}
</table>
您的javascript调用

$.ajax( {
       type:"POST",
       url:"/show_job_details/",
       data:{"test_name":'execution details'},
       success:function(response){
           // Append response.html where ever you need it in your html.
           console.log(response.html);
       }
});

如何在my.js文件的ajax块中复制或使用它。我已经在上面给出了my.js文件代码。@Ram我添加了基本的ajax post和success函数。您必须根据需要将其附加到任何元素。
def show_job_details(request):

     print "in job details"

     if request.POST and request.is_ajax():

                try:

    # fetching user and job details
                     print "in job details"
                     user_object_array=UserDetails.objects.all().order_by('email_id')
                     print "---------------------"
                     print user_object_array
                     job_object_array=JobDetails.objects.all().order_by('creation_date')

                     print "$$$$$ job details"
                     print job_object_array


                except IOError:
                        print "cant fetch data from jobdetails"

                return HttpResponse(job_object_array)



     else:
                raise Http404
<table>
{% for job in job_details %}
    <tr>
        <td>{{job.id}}</td>
        <td>{{job.user_id}}</td>
        <td>{{job.job_name}}</td>
        <td>{{job.start_time}}</td>
        <td>{{job.end_time}}</td>
        <td>{{job.job_url}}</td>
        <td>{{job.creation_date}}</td>
        <td>{{job.status}}</td>
    </tr>
{% endfor %}
</table>
import json
from django.template.loaders import render_to_string

def show_job_details(request):
    # Other logic
    job_object_array=JobDetails.objects.all().order_by('creation_date')
    data = {'html': render_to_string('table.html'), 'job_details':job_object_array}
    return HttpResponse(json.dumps(data), content_type='application/json')
$.ajax( {
       type:"POST",
       url:"/show_job_details/",
       data:{"test_name":'execution details'},
       success:function(response){
           // Append response.html where ever you need it in your html.
           console.log(response.html);
       }
});