1
votes

I have been receiving Forbidden (CSRF token missing or incorrect) I have tried different solutions found on Stack Overflow but I am unable to fix it! I have tried to use the render_to_response, and when I do that I receive this error:

This is usually caused by not using RequestContext. "A {% csrf_token %} was used in a template, but the context "

This is the forms.py

from django import forms


class AskForm(forms.Form):
    name = forms.CharField(
        max_length=30,
        widget=forms.TextInput(
            attrs={
                'placeholder': 'Write your name here',
                'class': 'ask-page-input',
                'label': 'Student Name',
            }
        ))
    email = forms.EmailField(
        max_length=254,
        widget=forms.EmailInput(
            attrs={
                'placeholder': 'Write your Email here',
                'class': 'ask-page-input',
                'label': 'Email',
            }
        ))
    subject = forms.CharField(
        max_length=50,
        widget=forms.TextInput(
            attrs={
                'placeholder': 'Write your Question or Related Subject here',
                'class': 'ask-page-input',
                'label': 'Subject',
            }
        ))

    message = forms.CharField(
        max_length=2000,
        widget=forms.Textarea(
            attrs={
                'placeholder': 'Write your message here',
                'class': 'ask-page-input',
                'label': 'Message',
            }
        ))

    source = forms.CharField(  # A hidden input for internal use
        max_length=50,  # tell from which page the user sent the message
        widget=forms.HiddenInput()
    )

    def clean(self):
        cleaned_data = super(ContactFrom, self).clean()
        name = cleaned_data.get('name')
        email = cleaned_data.get('email')
        subject = cleaned_data.get('subject')
        message = cleaned_data.get('message')
        if not name and not email and not subject and not message:
            raise forms.ValidationError('You have to write something!')

This is the views.py

from django.shortcuts import render, redirect, render_to_response
from forms import AskForm

def askpage_form(request):
    if request.method == 'POST':
        form = AskForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = AskForm()
    return render(request, "ask.html",  {'form': form,  })

This is the html file

  <!DOCTYPE html>
  <html lang="en" dir="ltr">
    <head>
      <meta charset="utf-8">
      <title>Physics Quizzes Ask</title>
    </head>
    <body>
      <form method="post">
        {% csrf_token %}
        {{ form }}
        <button type="submit" name="button">Submit</button>
      </form>
    </body>
  </html>
1
do you try with clearing cache?shafik
Yes, I have tried doing that but it didn't work! @ShafikurRahmanMajed Saaeddin
Is it availbale 'django.middleware.csrf.CsrfViewMiddleware' in MIDDLEWARE_CLASSES in settings.pyshafik
Yes, it is available.Majed Saaeddin

1 Answers

0
votes

In your form.py Change this

def clean(self):
    cleaned_data = super(ContactForm, self).clean()

To

def clean(self):
    cleaned_data = super(AskForm, self).clean()

My Forms shows like this: enter image description here