简体   繁体   中英

Pass data from JS to Python via Django

I want to send data from python to JS in Django. I successfully see the data in the response in the browser, but JS returns as empty string. What would be the reason?

trying to fetch information;

def XXX(request):
    message_id=request.POST.get('messageid')
    return render(request, 'XX.html')

sender;

    y = json.loads(response.text)
    ts = str(y["file"]["shares"]["public"]["XX"][0]["ts"])
    return render (request, 'problemduyurusucreate.html', {'ts':ts})

JS;

    <script type=text/javascript>
    function slacksil() {
  html2canvas(document.getElementById("main"), {
    letterRendering: 1,
    allowTaint: true,
    useCORS: true,})
  .then(function (canvas) {
  swal({
    title: "Emin misiniz?",
    text: "Duyuru Kanaldan Silinecek!",
    icon: "warning",
    buttons: true,
    dangerMode: true,})
  .then((willDelete) => {
  if (willDelete) {
    document.getElementById("result").src = canvas.toDataURL("image/png", 0.5);
    var deneme = "{{ ts }}";
    $.ajax({
    type: "POST",
    url: "{% url 'slacksilproblem' %}",
    data: { 
    "messageid": deneme,
    csrfmiddlewaretoken: '{{ csrf_token }}'}})
  swal({
    title: "Duyuru Kanaldan Silindi!",
      icon: "success",})} 
  else {
  swal("İşlem İptal Edildi!", {
    icon: "error",});}});
        })
        .catch((e) => {
            alert(e);
        }); 
}
  </script>

but I am displaying in my browser that I have successfully pulled the data在此处输入图像描述

first im sending post request to python for sending image after python script python send to html message timestamp id; 在此处输入图像描述

then post requst to delete message with timestamp id, but its comes empty to python. But when I look at the answer in the browser, I see that the data is coming在此处输入图像描述

I solved the problem by sending the data to the cookie with python and retrieving the data from again python.

sender data to cookie ( x is my data );

    html = HttpResponse("<h1>TP</h1>")
    html.set_cookie('ts', x, max_age = None)
    return html

and fetch data in another fonc. like this;

message_id=request.COOKIES['ts']

finally delete cookie;

html.delete_cookie('ts','!') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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