简体   繁体   中英

500 apache server error on django page with contact form

I create my first page in django 2.2. I have implemented it on the apache server, but I have a problem displaying the page with the contact form. I receive a 500 error in response. I use the send_mail function to send messages.

I checked the server logs. Unfortunately they don't tell me much. I added the {% csrf_token%} tag under the form tag in the html code. I think the problem is because of the wrong view for the page, but I don't know what to do to fix it.

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")

Below, I paste the apache server log:

[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"]

Some changes you need to have: You need to make sure that the request is POST before using post data and also better use the django forms instead of taking inputs from POST request

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM