简体   繁体   English

django中的window.open模板,使用chrome

[英]window.open template in django, using chrome

After reading everything on the internet, I'm no longer sure what the problem is, and I'd appreciate some help. 在阅读完互联网上的所有内容之后,我不再确定问题出在哪里,我将不胜感激。 Thanks. 谢谢。

I have a three different forms that the user is filling out, and I've put a jQuery UI dialog box in that comes up when the form is submitted. 我有三种不同的表单供用户填写,提交表单时会弹出一个jQuery UI对话框。 I'm trying to have the "next" button navigate the user away from the current page to the next template, but have not been able to make this work. 我正在尝试使“下一个”按钮将用户从当前页面导航到下一个模板,但是却无法完成这项工作。 I'm getting errors in Chrome that say "unexpected token ." 我在Chrome中收到错误消息,提示“意外令牌”。 on my window.open line, too, and I've tried using variables to set it instead, and have tried various ajax calls, but nothing seems to work. 在我的window.open行上,我也尝试使用变量来设置它,并尝试了各种ajax调用,但似乎没有任何效果。

Here's my url: 这是我的网址:

url(r'^campers/',campers, name="campers"),

And here's my javascript: 这是我的JavaScript:

    $("#vehiclebutton").click(function(e){
        e.preventDefault();
        $('<div></div>').appendTo('#vehicle_message')
            .html('<div><h3> Next, tell us about your sleeping arrangements. </h3></div>')
            .dialog({
                title: "Confirm",
                width: 500, 
                height: 300,
                modal: true,
                resizable: false,
                show: { effect: 'drop', direction: "left" }, 
                hide: {effect:'drop', direction:'left'},
                buttons: {
                    Next: function() {
                        $.ajax({ 
                            window.location.href('http://127.0.0.1:8000/campers')
                            $(this).dialog("close");
                        }) 
                }

            }
        })
    })
});

I propose a correction to your vehiclebutton click handler: 我建议对您的vehiclebutton单击处理程序进行更正:

 $(function () { $("#vehiclebutton").click(function(e){ e.preventDefault(); var obj = null; $('<div></div>').appendTo('#vehicle_message') .html('<div><h3> Next, tell us about your sleeping arrangements. </h3></div>') .dialog({ title: "Confirm", width: 500, height: 300, modal: true, resizable: false, show: {effect: 'drop', direction: "left"}, hide: {effect: 'drop', direction: 'left'}, buttons: [{ text: 'Next', click: function () { var data = ""; obj = $(this); $.ajax({ url: 'http://127.0.0.1:63342/campers', data: data, method: "POST", dataType: 'html', }).done(function(data, textStatus, jqXHR) { obj.dialog("close"); }).fail(function(jqXHR, textStatus) { obj.dialog("close"); }) } }] }); }); }); 
 <link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.min.css" rel="stylesheet"/> <script src="http://code.jquery.com/jquery-1.11.3.js"></script> <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script> <button id="vehiclebutton">Click Me</button> <p id="vehicle_message"></p> 

Ok, here's what finally worked. 好的,这就是最后的工作。 I'm not sure if there are some unnecessary parts to this code, but based on the answers above and on other parts of the internet, and the Django docs, I pieced this together: 我不确定该代码是否包含一些不必要的部分,但是基于上面的答案以及互联网的其他部分以及Django文档,我将其拼凑而成:

  1. I got the Chrome extension that allows for cross origin site resouce sharing. 我得到了Chrome扩展程序,该扩展程序允许跨源站点资源共享。

  2. I put in the csrf cookies per the Django docs . 我根据Django docs放入了csrf cookie。

  3. I added the ajax header to the ajax call itself. 我将ajax标头添加到了ajax调用本身。

All of this is pretty basic, but I haven't used ajax much, and not in conjunction with Django at all, and here's my working code. 所有这些都是非常基本的,但是我没有使用过很多ajax,也没有与Django一起使用,这是我的工作代码。 If it could be improved by edits, I'd love to know. 如果可以通过修改加以改善,我很想知道。

$(function () {
      $("#profilebutton").click(function(e){
        e.preventDefault();
        var obj = null;
        $('<div></div>').appendTo('#profile_message')
        .html('<div><h3> Next, tell us about how you\'re getting here. </h3></div>')
        .dialog({
          title: "Confirm",
          width: 500,
          height: 300,
          modal: true,
          resizable: false,
          show: {effect: 'drop', direction: "left"},
          hide: {effect: 'drop', direction: 'left'},
          buttons: [{
            text: 'Next',
            click: function () {
              var data = "";
              obj = $(this);
              $.ajax({
                method: "POST", 
                beforeSend: function(xhr, settings) {
                    if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                        xhr.setRequestHeader("X-CSRFToken", csrftoken);
                    }
                },
                data: data,
                dataType: 'html',
              }).done(function(data, textStatus, jqXHR) {
                window.location.href = '/vehicle/'; 
                obj.dialog("close");
              }).fail(function(jqXHR, textStatus) {
                obj.dialog("close");
              })
            }
          }]
        });
      });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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