1
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

我试图通过为表单添加 2 个按钮来更改我的模板并收到此错误。它显示两个按钮,但每次我单击一个按钮时,它都无法正确提交。我想根据按钮单击将模态中的 is_doctor 字段设置为 false 或 true。

视图.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]]

表格.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', )
    

选择用户类型

{% 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 %}

创建用户

{% 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 %}
4

1 回答 1

1

您正在使用表单向导,您的 html 表单中似乎缺少向导管理表单数据。

{% 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 %}

在这里您可以查看更多信息:https ://django-formtools.readthedocs.io/en/latest/wizard.html#creating-templates-for-the-forms

于 2022-01-01T21:30:18.907 回答