【问题标题】:Passing js variables to django view [duplicate]将js变量传递给django视图[重复]
【发布时间】:2011-11-28 02:26:50
【问题描述】:

我正在使用 jquery .submit() 函数来计算两个变量(f_amount 和 f_variables),但我不知道如何将它们传递给下一个 django 视图。

<form method="POST" action="/app/view">{% csrf_token %}                    
                    <select  id="fd" name="fd">
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="5">5</option>
                        <option value="1">1</option>
                    </select>

                <div id="demo2" class="demo">
                <ul>
                {% for f_type, fs in f.items %}
                    <li>
                        <a>{{ f_type }}</a>
                        <ul>
                            {% for f in fs %}
                                <li><a id="{{f.0}}">{{f.1}}</a></li>
                            {% endfor %}
                        </ul>
                    </li>
                {% endfor %}
                </ul>
                </div>
                <input id="plan" type="submit" value="sched it!" class="btn primary" />
        </form>

<script>
$("form").submit(function() {
                var checked_ids = new Array();
                var f_amount = $("select option:selected").val();
                $("#demo2").jstree("get_checked",null,true).each 
                    (function (index,value) {
                        if (value.children[2].id) { 
                            checked_ids.push(value.children[2].id); 
                        }
                    });
                var f_ids = checked_ids.join(" ");

 });
</script>

【问题讨论】:

  • 下一个 Django 视图?为什么不首先担心 current Django 视图。
  • 它们应该可以通过 request.POST querydict 对象从您的视图中访问。您可以在此处找到有关查询字典的更多信息:docs.djangoproject.com/en/dev/ref/request-response/…
  • 这是我在表单提交后加载的视图中得到的 QueryDict,变量 f_amount 和 f_ids 不在字典中:

标签: javascript jquery django forms


【解决方案1】:

我可以在 Django 2.1 中这样做

x123.html 首先在第一个 html 文件中创建一个像这样的隐藏输入字段

<form id="scoresForm" method="POST">
  {% csrf_token %}
  <input type="hidden" name="scores" value=""/>
</form>

views.py 然后在 views.py 中定义您的视图并进行 POST 数据处理

def result(request, league_id, season_id, matchday):
    scores = request.POST.get("scores", None)
    if scores != None:
        scores = scores.replace("&quot;", "'")
    #scores = scores[1:-1]
    scores = json.loads(scores)
    context = {
        'league_id': league_id,
        'season_id': season_id,
        'matchday': matchday,
        'scores': scores,
    }
    return render(request, 'matchstatistics/result.html', context)

x123.js 最后在属于您的第一个 html 模板的 JS 文件中

    var resultUrl = currentUrl + "/result/" + league_id + "/" + season_id + "/" + matchday + "/";
    var scoresForm = document.forms['scoresForm'];
    scoresForm.action = resultUrl;
    scoresForm.elements["scores"].value = JSON.stringify(scores);
    document.getElementById("scoresForm").submit();

y123.html 您可以通过调用 f.e. 在以下视图中轻松访问传递的数组/变量。 {{ score }} 在你的 Django 模板中

请随意优化代码,尤其是 JSON-Object 序列化和反序列化部分,我是 Django/Python/JavaScript 的新手 :)

【讨论】:

    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2012-12-13
    • 2020-07-11
    • 2015-09-12
    • 1970-01-01
    • 2012-08-20
    相关资源
    最近更新 更多