在视图PYTHON / DJANGO中调用脚本

时间:2019-12-17 00:17:59

标签: python django import python-import

我是Python和Django的新手,还是一般的代码

我创建了一个视图,该视图使用javascript从html模板获取价值。收集了这些值后,我想运行一个考虑这些新值的脚本

@csrf_exempt
def register(request):
      type_list=request.POST.get('selected_type').split("&") #array collected from checkboxes
      from_date=request.POST.get('start_date').replace("&","") #date collected
      to_date=request.POST.get('end_date').replace("&","") #date collected
      import transaction.query #script I want to run
      profiling = transactionprofiling.objects.all() #database/model updated with the script
      tmpl = loader.get_template("transaction/index.html")
      cont = {'profiling': profiling}
      return HttpResponse(tmpl.render(cont)) #return the model in the template

该定义不起作用,因为它不保存值“ type_list”,“ from_date”,“ to_date” 我做了一些研究但没有发现

预先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果有人对我找到的答案感兴趣,我创建了新模型并将结果保存在模型中,然后使用这些模型而不是变量来运行脚本!欢呼!

@csrf_exempt
def register(request):
      type_list=request.POST.get('selected_type').split("&")
      from_date=request.POST.get('start_date').replace("&","")
      to_date=request.POST.get('end_date').replace("&","")

      filtertype.objects.all().delete()

      n = 0
      for item in type_list:
            n = n + 1
            print(item)
            add = filtertype(id=n, transaction_type=item)
            add.save()

      filterdate.objects.all().delete()

      add = filterdate(id=3, start_date=from_date,end_date=to_date)
      add.save()

      print(type_list,from_date,to_date)

      import transaction.query

      profiling = transactionprofiling.objects.all()
      tmpl = loader.get_template("transaction/index.html")
      cont = {'profiling': profiling}
      return HttpResponse(tmpl.render(cont))