我正在尝试在同一页面上添加联系方式和登录表格,但是当在正确的表格中输入登录信息然后单击提交按钮时,会从联系表格必填字段中触发this field is required
我正在使用Django 2.1和postgreSQL。
我的观点:
def landing_view(request):
if request.method == 'GET':
contact_form = ContactForm()
login_form = CustomAuthForm(request.POST)
else:
contact_form = ContactForm(request.POST)
login_form = CustomAuthForm(request.POST)
if request.POST.get('submit') == 'contact_form':
if contact_form.is_valid():
name = contact_form.cleaned_data['name']
phone = contact_form.cleaned_data['phone']
from_email = contact_form.cleaned_data['from_email']
message = contact_form.cleaned_data['message']
subject = name + " Contact Beta Landing"
msg = "\n \n Mail: " + str(from_email) + \
"\n \n Phone: " + str(phone) + \
"\n \n Message: \n \n" + message
from_send_mail = "'\"" + \
name + \
"\" <contactBeta@ekiter.com>'"
try:
send_mail(
subject,
msg,
from_send_mail,
['contact@ekiter.com'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
messages.add_message(request, messages.SUCCESS,
'Success! Thank you for your message, we will contact you.')
return redirect('landing')
elif request.POST.get('submit') == 'login_form':
if login_form.is_valid():
username = login_form.cleaned_data['username']
password = login_form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('home')
else:
return redirect('landing')
return render(request, "../templates/landing.html", {'form': contact_form, 'loginForm': login_form})
这是联系表代码:
<form method="post">
{% csrf_token %}
{{ form.media }}
<div class="row">
<div class="col-lg-6">
{{ form.name|as_crispy_field }}
{{ form.from_email|as_crispy_field }}
{{ form.phone|as_crispy_field }}
</div>
<div class="col-lg-6">
{{ form.message|as_crispy_field }}
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button class="btn btn-primary btn-xl text-uppercase" type="submit"
name="submit" value="contact_form">
Send Message
</button>
</div>
</div>
</form>
这是“登录”表单:
<form method="post">
{% csrf_token %}
{{ loginForm.media }}
<div class="row">
<div class="col-lg-6">
{{ loginForm.username|as_crispy_field }}
{{ loginForm.password|as_crispy_field }}
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button class="btn btn-primary text-uppercase" type="submit"
name="submit" value="login_form">
Login
</button>
</div>
</div>
</form>
我的Forms.py: 我添加此代码是为了防止您想检查是否有问题,但是我可以确保它们都能正常工作。 y试图将它们放在同一页上的问题。
class CustomAuthForm(AuthenticationForm):
username = forms.CharField(widget=TextInput(attrs={'class': 'validate', 'placeholder': 'User *'}))
password = forms.CharField(widget=PasswordInput(attrs={'placeholder': 'Password *'}))
class ContactForm(forms.Form):
from_email = forms.EmailField(required=True)
name = forms.CharField(required=True)
phone = forms.IntegerField(required=False)
message = forms.CharField(widget=forms.Textarea, required=True)
def __init__(self, *args, **kwargs):
# first call parent's constructor
super(ContactForm, self).__init__(*args, **kwargs)
# there's a `fields` property now
self.fields['from_email'].required = True
self.fields['name'].required = True
self.fields['phone'].required = False
self.fields['message'].required = True
self.fields['from_email'].widget = forms.TextInput(
attrs={'placeholder': 'Your Email *'})
self.fields['name'].widget = forms.TextInput(
attrs={'maxlength': '40',
'placeholder': 'Your Name *'})
self.fields['phone'].widget = forms.TextInput(
attrs={'maxlength': '15',
'placeholder': 'Your Phone (Optional)'})
self.fields['message'].widget = forms.Textarea(
attrs={'rows': '2',
'maxlength': '5000',
'class': 'textarea-limited',
'placeholder': 'Your Message *'})
# evade all labels and help text to appear when using "as_crispy_tag"
self.helper = FormHelper(self)
self.helper.form_show_labels = False
self.helper._help_text_inline = True
我认为问题已经解决,但是我不知道如何解决。我试图将所有相关问题应用于StackOverFlow,但无法解决。我通常使用预定义的Django视图,这是我的第一个“复杂”请求视图。
使用此代码,联系表单可以完美地工作(我接受更好的结构化发送邮件方式),问题是,正如我在问题开头提到的那样,当我填写登录表单时。
谢谢你的建议。