我正在尝试运行django表单,但是即使提交表单也每次都将它作为GET方法运行,但它也会获取GET。 我一直在寻找解决方案,但是没有运气。
这是来自vs代码的服务器日志
[31/Oct/2019 16:15:13] "GET /static/bootstrap.min.js.map HTTP/1.1" 404 1684
[31/Oct/2019 16:15:13] "GET /static/bootstrap.min.css.map HTTP/1.1" 404 1687
<<<<<< THIS IS GET METHOD >>>>>>>>>>>
[31/Oct/2019 16:15:14] "GET /forgotpassword/ HTTP/1.1" 200 1808
[31/Oct/2019 16:15:14] "GET /static/bootstrap.min.js.map HTTP/1.1" 404 1684
[31/Oct/2019 16:15:14] "GET /static/bootstrap.min.css.map HTTP/1.1" 404 1687
[31/Oct/2019 16:15:17] "POST /successforgotpassword/ HTTP/1.1" 200 286
这是我的 models.py
from django import forms
class ForgotPasswordForm(forms.Form):
mail = forms.EmailField(label="EMAILL - ID", label_suffix="*", max_length=50, min_length=4, widget=forms.EmailInput(
attrs={"class": "form-control", "placeholder": "Enter Email Address"}))
def clean_mail(self):
passed_data1 = self.cleaned_data.get("mail")
passed_data2 = self.cleand_data["mail"]
print(passed_data1, passed_data2)
req_data = "abc@gmail.com"
if passed_data1 == req_data:
raise forms.ValidationError("boss no gmail , why!!!")
if passed_data1 == "":
raise forms.ValidationError("boss no gmail , why!!!")
return passed_data1
这是我的 forgotpassword.html
<body>
<div class="container" style="margin-top: 100px;">
<form action="/successforgotpassword/" method="POST" class="form-horizontal" enctype="multipart/form-data">
{% csrf_token %}
<div class="jumbotron boxing">
<p style="text-align:center; background-color: rgb(246,
250, 8); color: rgb(250, 19, 19); font-size: 18px;">
Please provide your valid email address , We are going to send mail
to mentioned email address , So that you can reset your password
</p>
<br /><br />
<div class="form-group">
{% for fld in frgpwdform %}
{{ fld.label }}
{{ fld }}
{% endfor%}
</div>
<div class="alert-danger">
{% if frgpwdform.errors %} {% for err in frgpwdform.errors%}
<p class="alert-danger">{{ err }}</p>
{% endfor %} {%endif %}
</div>
<br />
<div class="form-group">
<input type="submit" class="btn btn-primary btn-lg" value="SEND RESET LINK TO EMAIL ADDRESS" />
<a href="/register" class="nav-link">
<h3>Register?</h3>
</a>
</div>
</div>
</form>
</div>
</body>
这是我的 views.py
from django.shortcuts import render, redirect
from .models import ForgotPasswordForm
def forgotpassword(request):
if request.method == "POST":
print("<<<<<< THIS IS POST METHOD >>>>>>>>>>>")
frgpwdform = ForgotPasswordForm(request.POST)
passed_value = request.cleand_data.get("mail")
print(passed_value)
if frgpwdform.is_valid():
print("<<<<<< THIS IS VALID >>>>>>>>>>>")
return render(request, "successforgotpassword.html", {'frgpwdform': frgpwdform})
else:
print("<<<<<< THIS IS INVALID >>>>>>>>>>>")
return render(request, "forgotpassword.html", {'frgpwdform': frgpwdform})
else:
frgpwdform = ForgotPasswordForm()
print("<<<<<< THIS IS GET METHOD >>>>>>>>>>>")
return render(request, "successforgotpassword.html", {'frgpwdform': frgpwdform})
return render(request, "forgotpassword.html", {'frgpwdform': frgpwdform})
def successforgotpassword(request):
return render(request, "successforgotpassword.html")
这是我的 successforgotpassword.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Success Forgot Password</title>
</head>
<body>
{{frgpwdform}}
{{frgpwdform.email}}
</body>
</html>
这是 urls.py
from django.urls import path
from . import views
urlpatterns = [
path("forgotpassword/", views.forgotpassword),
path("successforgotpassword/", views.successforgotpassword),
]
答案 0 :(得分:0)
您的方法行为正确
<<<<<< THIS IS GET METHOD - SHOW FORM >>>>>>>>>>>
[31/Oct/2019 16:15:14] "GET /forgotpassword/ HTTP/1.1" 200 1808
<<<<<< THIS IS POST METHOD - SUBMIT FORM >>>>>>>>>>>
[31/Oct/2019 16:15:17] "POST /successforgotpassword/ HTTP/1.1" 200 286
尝试以下解决方案:
最重要的更改是在action =“ / 忘记密码 /”中,而在action =“ / 成功忘记密码 /”
<form action="/forgotpassword/" method="POST" class="form-horizontal">
views.py
from django.shortcuts import render, redirect
from .models import *
def forgotpassword(request):
"""
Form to request password change
:param request:
:return:
"""
form = ForgotPasswordForm()
if request.method == "POST":
print("<<<<<< THIS IS POST METHOD >>>>>>>>>>>")
form = ForgotPasswordForm(request.POST)
if form.is_valid():
print("<<<<<< THIS IS VALID FORM >>>>>>>>>>>")
mail = form.cleaned_data.get("mail")
# Your logic goes here
return redirect('/successforgotpassword')
return render(request, "forgotpassword.html", {'form': form})
def successforgotpassword(request):
"""
Success message screen
:param request:
:return:
"""
return render(request, "successforgotpassword.html")
forgotpassword.html
<form action="/forgotpassword/" method="POST" class="form-horizontal">
{% csrf_token %}
<div class="form-group {% if form.mail.errors %}has-error{% endif %}">
{{ form.mail }}
{% if form.mail.help_text %}
<span class="help-block">{{ form.mail.help_text }}</span>
{% endif %}
{% for error in form.mail.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-lg" value="SEND RESET LINK TO EMAIL ADDRESS" />
<a href="/register" class="nav-link">
<h3>Register?</h3>
</a>
</div>
</form>
successforgotpassword.html
<body>
<p>Check your mailbox to reset the password</p>
</body>