将表单发布到Django时设置request.session会导致HTTP 500错误

时间:2015-10-02 20:33:45

标签: django

即使将会话设置为简单字符串,也会将http响应从状态200更改为500. httml按钮具有id =“requestquote”。 网页javascript:

$('#requestquote').click(function(){
    var jpicks = JSON.stringify(picks);
    $.post("requestforquote",{counties:jpicks}).done(function(){
        window.location = BASE_URL + "countyparcels/quoteform"
    });
});

url.py

urlpatterns = [
    url(r'^(requestforquote)$',views.request4quote),
    url(r'^(quoteform)$',views.quoteform)
]

views.py

SHOPPING_CART = 'shoppingcart'

@csrf_exempt
def request4quote(request,misc):
    try:
        request.session[SHOPPING_CART] = request.POST['counties']  # No    exception here
    except Exception as e:
        print e.message
    try:
        response = HttpResponse("True",content_type="text/plain")  # No exception here
    except Exception as e:
        print e.message
        response = Http404
    return response  # Get HTTP: 500  if I include session setting code above
        # I get the error even is session set to simple string literal ... like "true"

2 个答案:

答案 0 :(得分:1)

问题是我没有同步我的数据库。我没有其他东西,身份验证和会话表。执行以下操作修复了问题:

<div class="checkbox">
    <label>
        <input type="checkbox" value="Clothing" name="Child1GiftTypeNeed" checked>
            Clothing
    </label>
</div>
<div class="checkbox">
    <label>
        <input type="checkbox" value="Shoes" name="Child1GiftTypeNeed">
            Shoes
    </label>
</div>
<div class="checkbox">
    <label>
        <input type="checkbox" value="Coat/Jacket" name="Child1GiftTypeNeed">
            Coat/Jacket
    </label>
</div>
<div class="checkbox">
    <label>
        <input type="checkbox" value="Books" name="Child1GiftTypeNeed">
        Books
    </label>
</div>

答案 1 :(得分:0)

错误发生在您的响应变量值中 您将在异常中返回Http404类型对象,这将引发AttributeError,因此它是导致另一个异常的异常。

从您上面提供的代码中我唯一能想到的是您没有导入 HttpResponse
你需要 : from django.http import HttpResponse

如果这不起作用,则需要提供追溯。