首页 > 解决方案 > django 页面上的 500 apache 服务器错误,带有联系表

问题描述

我在 django 2.2 中创建了我的第一页。我已经在 apache 服务器上实现了它,但是我在显示带有联系表单的页面时遇到了问题。我收到 500 错误作为响应。我使用 send_mail 函数发送消息。

我检查了服务器日志。不幸的是,他们并没有告诉我太多。我在html代码的form标签下添加了{% csrf_token%}标签。我认为问题是由于页面的错误视图,但我不知道该怎么做才能修复它。

def contact(request):
    message = request.POST.get('message', False)
    sender = request.POST.get('email', False)
    subject = "New message from example.com from: " + sender

    send_mail(subject, message, 'contact@example2.com', ['contact@example3.com'], fail_silently=False)

    return render(request=request, template_name="main/contact.html")

下面,我粘贴 apache 服务器日志:

[Thu Oct 24 10:47:28.394512 2019] [:error] [pid 17558] [client x.x.x.x] ModSecurity: Warning. Pattern match "^5\\\\d{2}$" at RESPONSE_STATUS. [file "/usr/share/modsecurity-crs/activated_rules/modsecurity_crs_50_outbound.conf"] [line "53"] [id "970901"] [rev "2"] [msg "The application is not available"] [data "Matched Data: 500 found within RESPONSE_STATUS: 500"] [severity "ERROR"] [ver "OWASP_CRS/2.2.9"] [maturity "9"] [accuracy "9"] [tag "WASCTC/WASC-13"] [tag "OWASP_TOP_10/A6"] [tag "PCI/6.5.6"] [hostname "example.com"] [uri "/contact/"] [unique_id "XbFlIH8AAQEAAESWGb4AAAAA"]
[Thu Oct 24 10:47:28.397631 2019] [:error] [pid 17558] [client x.x.x.x] ModSecurity: Warning. Operator GE matched 4 at TX:outbound_anomaly_score. [file "/usr/share/modsecurity-crs/activated_rules/modsecurity_crs_60_correlation.conf"] [line "40"] [id "981205"] [msg "Outbound Anomaly Score Exceeded (score 4): The application is not available"] [hostname "example.com"] [uri "/contact/"] [unique_id "XbFlIH8AAQEAAESWGb4AAAAA"]

标签: pythondjangoapache

解决方案


您需要进行一些更改:您需要确保请求是POST在使用发布数据之前,并且更好地使用django 表单 而不是从 POST 请求中获取输入

def contact(request):
     if request.method == 'POST'
        message = request.POST.get('message', False)
        sender = request.POST.get('email', False)
        subject = "New message from example.com from: " + sender

        send_mail(subject, message, 'contact@example2.com', ['contact@example3.com'], fail_silently=False)


      return render(request=request, template_name="main/contact.html")

推荐阅读