我正在尝试将芹菜任务中的数据输出到一个单独的窗口中.我是
JavaScript和AJAX的新手,这是我当前的问题所在.执行视图后,启动芹菜任务并呈现下一个html页面(success.html):
success.html
{% block content %} <body> {% if task_id %} <h1>task_id has been called: {{ task_id }}</h1> <script src="{% static 'MyAPP/bootstrap/js/task_output_retrieval.js' %}"></script> <script type='text/javascript'> task_state("{{ task_id }}"); </script> <script src="{% static 'MyAPP/bootstrap/js/update-hello-user.js' %}"></script> <script type='text/javascript'> second(); </script> <h1> END </h1> {% endif %} </body> {% endblock content %}
我知道JavaScript被调用,因为窗口至少是打开的.这是.js:
task_output_retrieval.js
function task_state (task_id) { var taskID = task_id; var newWin = window.open('','new window','width=200,height=100'); $.ajax({ url: '{% url validate_task_state %}',data: {'taskID':taskID},method: 'POST',dataType : "json",success: function(data){ $(newWin.document.body).html(data); newWin.document.write(data); newWin.document.close(); newWin.focus(); newWin.print(); newWin.close(); },error: function (){ alert('An error occured'); } }); } task_state(task_id);
而url.py:
url(r'^ajax/task_state/$',task_state,name='validate_task_state'),# for ajax
并且观点:
admin_scripts.py
def task_state(request): print ("You reached the task_state function") data = 'Fail' task_id = request.GET.get('task_id') #task_id = request.session['task_id'] try: async_result = AsyncResult(task_id) except KeyError: ret = {'error':'No optimisation (or you may have disabled cookies).'} return HttpResponse(json.dumps(ret)) print ("request.is_ajax(): {0}".format(request.is_ajax())) if request.is_ajax(): if 'task_id' in request.POST.keys() and request.POST['task_id']: task_id = request.POST['task_id'] async_result.get() data = { 'state': async_result.state,'result': async_result.result,} #data = async_result.result or async_result.state print ("data:{0}".format(data)) else: data = 'No task_id in the request' else: raise SuspicIoUsOperation("This is not an ajax request.") json_data = json.dumps(data) return HttpResponse(json_data,content_type='application/json')
task_state中仍有许多未解决的问题我还没有完全理解,通过反复试验,我会到达那里,但是现在,task_state没有被调用.我怀疑的问题是AJAX调用(“url”),但我无法弄清楚原因.我哪里错了?
更新:选中“JS Test Stuff”复选框后,将呈现success.html,无错误.从success.html中调用AJAX JavaScript(task_output_retrieval.js),这是经过验证的,因为我从success.html调用了2个JavaScript文件(另一个是update-hello-user.js).打开task_output_retrieval.js的窗口,并显示update-hello-user.js的弹出窗口.它在我调用视图的task_output_retrieval.js中:
$.ajax({ url: query_url,)
但这没有呈现.
这是控制台的输出:
[17/Aug/2018 04:59:12] INFO [django.server:124] "GET /MyApp/opt/ HTTP/1.1" 200 6631 async_result f2224e67-3e47-4980-9dc8-58622928e090 TASK_ID f2224e67-3e47-4980-9dc8-58622928e090 [17/Aug/2018 04:59:14] INFO [django.server:124] "POST /MyApp/opt/ HTTP/1.1" 200 6412 [17/Aug/2018 04:59:14] INFO [django.server:124] "GET /MyAppsite-static/MyApp/bootstrap/js/update-hello-user.js HTTP/1.1" 200 52 [17/Aug/2018 04:59:14] INFO [django.server:124] "GET /MyAppsite-static/MyApp/bootstrap/js/task_output_retrieval.js HTTP/1.1" 200 640
解决方法
我在查看您的代码时遇到的问题是您在JavaScript文件中使用{%url validate_task_state%}.如果您按照最常用的建议方法来设置Django并提供其静态内容,则模板引擎将不会处理您的JavaScript文件,并且不会处理该模板标记.此外,它需要围绕其参数引用所以{%’url validate_task_state’%}
您应该更改success.html模板,将需要的URL传递给task_state函数,如下所示:
<script src="{% static 'MyAPP/bootstrap/js/task_output_retrieval.js' %}"></script> <script type='text/javascript'> task_state("{% url 'validate_task_state' %}","{{ task_id }}"); </script>
function task_state (query_url,task_id) { var taskID = task_id; var newWin = window.open('',height=100'); $.ajax({ url: query_url,error: function (){ alert('An error occured'); } }); }
您在评论中说Django没有看到请求.常见问题是CSRF保护:自定义POST请求需要传递CSRF令牌,否则将被拒绝.有关如何操作的详细信息部分取决于您的具体配置,但一般来说,我这样做:
// Grab the CSRF token from the cookie. var csrftoken = $.cookie('csrftoken'); $.ajax({ type: "POST",url: "... my url ...",headers: { // Pass the token with the query. 'X-CSRFToken': csrftoken },// other ajax options... });