Django视图没有从帖子表单接收变量

时间:2017-09-13 12:37:40

标签: javascript python html django

我通过帖子表单将变量从一个模板发送到视图,然后在下一个模板中使用它们。但是当变量到达模板时,变量不存在。

这是第一个模板中使用的JavaScript。 game2.html:

function clickFntn(clicked_id) {
{% autoescape off %}
  var clicked = document.getElementById(clicked_id).src;
  var clicked1 = clicked.replace(/^.*[\\\/]/, '');
  var clicked2 = clicked1.slice(0,-4);
  if (clicked2 == gv_common) {
    gv_set1_counter = gv_set1_counter + 1;
    var set1 = {{ set1 }};
    if (gv_set1_counter == 2) {
      var_user = "{{user}}";
      game_id = {{game.id}};
      var player_end_time = new Date();
      var play_time = parseInt((player_end_time-player_start_time)/1000);
      post("{% url 'game2_end' %}", {type:"first", user:var_user, time: play_time, id: game_id});
    }
{% endautoescape %}
};
function post(path, params, method) {
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.getElementById("form1");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }

    document.body.appendChild(form);
    form.submit();
}

view.py:

def game2_end(request):
    if request.method == "POST":
        if request.POST.get("type") == "first":
            user_var = request.POST.get("user")
            id_var = int(request.POST.get("id"))
            game=get_object_or_404(mt_game, pk=id_var)
            if (request.user == game.f_player1):
                p1_time_var = int(request.POST.get("time"))
                p2_time_var = game.f_p2_play_time
                mt_game.objects.filter(pk=id_var).update(f_p1_play_time=2)
                return render(request, 'game2_end.html', {'user_var':user_var, 'id_var': id_var, 'p1_time_var':p1_time_var, 'p2_time_var':p2_time_var})
            elif (request.user == game.f_player2):
                p1_time_var = game.f_p1_play_time
                p2_time_var = int(request.POST.get("time"))
                mt_game.objects.filter(pk=id_var).update(f_p2_play_time=p2_time_var)
                return render(request, 'game2_end.html', {'user_var':user_var, 'id_var': id_var, 'p1_time_var':p2_time_var, 'p2_time_var':p1_time_var})

第二个模板文件:game2_end.html

<p>Game id: {{id_var}}</p>
<p>User: {{user_var}} </p>
<p>Your Gameplay Duration: {{p1_time_var}} seconds </p>
<p>Opponent Gameplay Duration: {{p2_time_var}} seconds </p>

我得到的浏览器输出是:

Game id: 23

User: ""

Your Gameplay Duration: seconds

Opponent Gameplay Duration: seconds 

缺少除id_var以外的所有变量。 我已检查变量是否在发布请求之前的第一个模板中可用,并且它们是。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

事实证明request.user正在为我的用户名添加一个尾随空格。用str(request.user).strip()替换它,它就像一个魅力。