Upon hitting the submit button in the login form, I encountered the following error message:
Forbidden (403) CSRF verification failed. Request aborted. CSRF token missing or incorrect.
settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
views.py
def contact_us(request):
if request.method == "POST":
con_name = request.POST['con_name']
con_email = request.POST['con_email']
con_company = request.POST['con_company']
inquiry = request.POST['inquiry']
con_message = request.POST['con_message']
#context = RequestContext(request)
#context_dict={'con_name':con_name}
#context_dict.update(csrf(request))
return render(request, 'contact_us.html', {'con_name':con_name})
else:
return render(request, 'contact_us.html',{})
contact_us.html
<form id="contact-form" action="{% url 'contact_us' %}" method="post">
{% csrf_token %}
<div class="contact-form">
<div class="contact-input">
<div class="contact-inner">
<input name="con_name" type="text" placeholder="Name *">
</div>
<div class="contact-inner">
<input name="con_email" type="email" placeholder="Email *">
</div>
</div>
<div class="contact-inner">
<input name="con_company" type="text" placeholder="Company">
</div>
<div class="contact-select">
<div class="form-item contact-inner">
<span class="inquiry">
<select name="inquiry" class="select-item">
<option value="Your inquiry for">Your inquiry for</option>
<option value="General Information Request">General Information Request</option>
<option value="Partner Relations">Public Relations</option>
<option value="Digital Marketing">Digital Marketing</option>
<option value="Influencer Marketing">Influencer Marketing</option>
<option value="Brand Creation">Brand Creation</option>
<option value="Careers">Careers</option>
<option value="Brand Creation">Web Development</option>
<option value="Others">Others</option>
</select>
</span>
</div>
</div>
<div class="contact-inner contact-message">
<textarea name="con_message" placeholder="Please describe what you need."></textarea>
</div>
<div class="submit-btn mt-20">
<button class="ht-btn ht-btn-md" type="submit">Send message</button>
<p class="form-messege"></p>
</div>
</div>
</form>
I am relatively new to Django and web development, and I am struggling to identify the root of this issue. What steps can I take to resolve the CSRF verification error? Can someone provide guidance on how to fix this problem?