【问题标题】:Django App deployed to Heroku - psycopg2.errors.NotNullViolation as data is not passed through Ajax部署到 Heroku 的 Django 应用程序 - psycopg2.errors.NotNullViolation 因为数据未通过 Ajax 传递
【发布时间】:2020-04-17 10:25:49
【问题描述】:

我已成功将我的应用程序部署到 Heroku,但一项功能无法正常运行,尽管它在我的本地环境中运行良好。从 Heroku 日志中,我可以看到以下内容:

2020-04-17T09:50:34.538062+00:00 app[web.1]: Internal Server Error: /shop/add_to_cart/11/
2020-04-17T09:50:34.538078+00:00 app[web.1]: Traceback (most recent call last):
2020-04-17T09:50:34.538079+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
2020-04-17T09:50:34.538079+00:00 app[web.1]: return self.cursor.execute(sql, params)
2020-04-17T09:50:34.538080+00:00 app[web.1]: psycopg2.errors.NotNullViolation: null value in column "quantity" violates not-null constraint
2020-04-17T09:50:34.538081+00:00 app[web.1]: DETAIL:  Failing row contains (23, null, null, 23, 11).

提到的数量是通过ajax传递的:

$('.add-product-to-cart').on('click', function (e) {
    e.preventDefault();
    let product_id = $(this).attr('data-product-id');
    let quantity = $('#input-product' + product_id).val();
    let csrftoken = $('input[name=csrfmiddlewaretoken]').val();

    $.ajax({
        url: 'add_to_cart/' + product_id + '/',
        type: 'POST',
        data: {
            'product_id': product_id,
            'qty': quantity,
            'csrfmiddlewaretoken': csrftoken,
            },
        success: function (data) {
            alert("Produkt dodany");
        }

        });
    });

此代码同样适用于本地。 ajax 引用的 html 位如下:

<div class="bottom-area d-flex px-1">
    <div class="m-auto w-100">
        <form method="POST" action="{% url 'shop:add_to_cart' product.id %}"
              class="d-flex align-items-center justify-content-center"
                    id="add-product-to-cart-form">
            {% csrf_token %}
            <div class="d-flex align-items-center justify-content-start pr-2">
                <input type="number" name="qty"
                       id="input-product{{ product.id }}"
                       class="quantity input-number py-0"
                       value="{{ product.min_qty_value }}"
                       min="{{ product.min_qty_value }}"
                       max="500"
                       step="{{ product.min_qty_value }}"
                       style="padding: 0 0 0 10px; border-radius: 4px;
                              border: 1px solid black; width: 70px">
                <p class="mb-0 pl-1 price">
                    {{ product.get_min_qty_info_display }}
                </p>
            </div>
            <button type="submit" style="border-radius: 4px;background: #82ae46; border: none"
                    data-product-id="{{ product.id }}"
                    class="add-product-to-cart">
                <span><i class="ion-ios-cart" style="color: white"></i></span>
            </button>
        </form>
    </div>
</div>

您有什么想法,为什么当我单击表单按钮时,空值仅在我的 Heroku 应用程序(而不是本地)中传递?任何有关调试的帮助也将不胜感激。

【问题讨论】:

  • 它说你模型中的quantity字段不能是空值?不确定如何处理保存请求,需要更多信息。但在高层次上,您形成的输入名称是qty,模型字段名称是quantity,也许更改名称可以解决问题,但需要更多关于如何在 django 中处理保存请求的信息。
  • #Shoaib - 在视图中,我只是将 POST 值读取为 ,然后将其分配给 。如前所述,我在本地使用相同的模型/视图并且它工作正常。我还检查了 console.log 的数量 它正在从输入字段传递一个值。在我看来,相同的值没有传递给 ajax,后来又传递给 db,但也许我正在寻找错误的方向:/
  • 如果您也可以分享您的 api 视图代码,那将会很有帮助。或者在保存之前尝试在日志中打印qty 变量和cart_item 对象,看看里面的值是多少!

标签: django ajax heroku internal-server-error


【解决方案1】:

看来我需要在这里发布一个问题才能弄清楚。解决方案是执行 CLI 命令 。我没有连接点,因为我之前对表格进行了调整,但它们没有在 heroku postgre 表格上重新运行! 希望它对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2017-10-31
    • 1970-01-01
    • 2012-05-22
    • 2016-11-16
    • 2018-10-18
    • 2021-06-11
    • 2020-04-09
    • 2020-08-08
    相关资源
    最近更新 更多