django - 我不明白为什么我的表单没有在 django 中验证
问题描述
我还是 django 的新手。玩了一个leadmanager应用程序,我不知道为什么我的表单没有验证。
意见
def index(request):
lead=LeadForm()
if request.method == 'POST':
lead=LeadForm(request.POST)
if lead.is_valid():
messages.success(request, f'Thank you for registering. Someone will be contacting you soon.')
return redirect('index')
else:
lead=LeadForm()
messages.error(request, f'Something went wrong. Please try again later.')
return render(request, "frontend/index.html", {'lead':lead})
在 index.html 中
<form action="" method="POST" class="lead-form">
{% csrf_token %}
<fieldset class="lead-info">
<div class="form-control">
<label for="">Full Name</label>
{{ lead.fullname }}
</div>
<div class="form-control">
<label for="">Email</label>
{{ lead.email }}
</div>
<div class="form-control">
<label for="">Phone</label>
{{ lead.phone }}
</div>
<div class="form-control">
<label for="">City</label>
{{ lead.city }}
</div>
</fieldset>
<button type="submit" class="btn-pill">Submit</button>
</form>
在forms.py中
class LeadForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = Lead
fields = ['fullname', 'email', 'phone', 'city', 'contact_preference']
widgets = {'contact_preference': forms.RadioSelect }
任何帮助表示赞赏。contact_preference 正在渲染仅供参考,我只是剪掉了代码以使这个问题不那么长。
解决方案
推荐阅读
- javascript - 单击和确认时更改按钮的颜色
- python - 在 Django 查询集中动态定义过滤器参数
- typescript - react-scripts 从 3.4 更新到 4.0 导致 Jest 版本错误
- cytoscape.js - 在 Windows 10 中安装 Cytoscape 3.8.2 时出错
- c++ - C ++创建具有非常量大小的二维数组,
- flutter - 在 Flutter 中监听 Stream 并只向 DB 写入一次
- javascript - 我的 jquery 代码不适用于使用 ajax 更改页面
- python - Bottle and Mysql blocking request
- python - 查找所有依赖于 linux 系统库的 Python 包
- c++ - std::string 和向量之间的区别
在 C++ 中