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 %}
uj5u.com熱心網友回復:
您正在使用表單向導,但您的 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/400916.html
上一篇:django.core.exceptions.ImproperlyConfigured:設定DATABASE_URL環境變數
