Home > OS >  Django customising template for form
Django customising template for form

Time:01-02

SuspiciousOperation at /signup/
ManagementForm data is missing or has been tampered.
Request Method: POST
Request URL:    http://localhost:8000/signup/
Django Version: 3.2.9
Exception Type: SuspiciousOperation
Exception Value:    
ManagementForm data is missing or has been tampered.
Exception Location: C:\Users\arund\Desktop\Code\Django\portfolio-project\venv\lib\site-packages\formtools\wizard\views.py, line 282, in post

I was trying to change my templates by adding 2 buttons for the forms and got this error. It displays the two buttons but everytime I click one it doesn't submit correctly. I want to make the is_doctor field in the modal set to false or true depending on the button click.

views.py

from django.shortcuts import redirect, render
from .forms import PickUserType, SignUpForm, UserProfileForm
from django.contrib.auth import login, authenticate

from formtools.wizard.views import SessionWizardView

def show_message_form_condition(wizard):
    # try to get the cleaned data of step 1
    cleaned_data = wizard.get_cleaned_data_for_step('0') or {}
    # check if the field isDoctor was checked.
    return cleaned_data.get('is_doctor')== False

def process_data(form_list):
    form_data = [form.cleaned_data for form in form_list]
    print(form_data)
    return form_data

WIZARD_FORMS = [("0", PickUserType),
                ("1" , SignUpForm),
            ]
TEMPLATES = {"0": "pickUserType.html",
             "1": "createUser.html"}

class ContactWizard(SessionWizardView):

    def done(self, form_list, **kwargs):
        process_data(form_list)
        return redirect('home')

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

forms.py

from django import forms
from django.core.files.images import get_image_dimensions
from django.forms.widgets import RadioSelect
from pages.models import Profile
from django.contrib.auth.forms import UserCreationForm
from datetime import date, timedelta

class PickUserType(forms.Form):
    is_doctor = forms.BooleanField(required=False)

# verified = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'doctor'))
# from django.core.files.storage import FileSystemStorage

class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')

    class Meta:
        model = Profile
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )
    

pickUserType

{% block content %}

    <form method = "POST" action='.' enctype="multipart/form-data">
        {% csrf_token %}
        <button type="submit" value="Doctor" name="is_doctor">Doc</button>
        <button type="submit" value="User" name="is_doctor">User</button>
    </form>
    <a href="{% url 'login' %}">Log In</a>

{% endblock %}

createUser

{% block content %}

  <form method="post">
    {% csrf_token %}
    {% for field in form %}
      <p>
        {{ field.label_tag }}<br>
        {{ field }}
        {% if field.help_text %}
          <small style="color: grey">{{ field.help_text }}</small>
        {% endif %}
        {% for error in field.errors %}
          <p style="color: red">{{ error }}</p>
        {% endfor %}
      </p>
    {% endfor %}
    <button type="submit">Sign up</button>
    <a href="{% url 'login' %}">Log In</a>
  </form>
  
{% endblock %}

CodePudding user response:

You are using a formwizard and it seems that you are missing the wizard management formdata in your html form.

{% block content %}

  <form method="post">
    {% csrf_token %}
    <!-- A formwizard needs this form -->
    {{ wizard.management_form }}


    {% for field in form %}
      <p>
        {{ field.label_tag }}<br>
        {{ field }}
        {% if field.help_text %}
          <small style="color: grey">{{ field.help_text }}</small>
        {% endif %}
        {% for error in field.errors %}
          <p style="color: red">{{ error }}</p>
        {% endfor %}
      </p>
    {% endfor %}
    <button type="submit">Sign up</button>
    <a href="{% url 'login' %}">Log In</a>
  </form>
  
{% endblock %}

Here you check more: https://django-formtools.readthedocs.io/en/latest/wizard.html#creating-templates-for-the-forms

  • Related