如何在不使用Django表单的情况下验证Django中的模板版本化表单字段? [英] How to validate template versioned form fields in django without using django forms?
问题描述
我编写了一个小型身份验证应用程序,在其中使用了手动模板渲染.我可以从表单中获取和更新数据,但我只想验证字段.但是我无法前进,因为我没有使用任何Django表单模型.客户端验证正在进行中,但是服务器端验证又如何呢?
I wrote a small authentication application where i used manual template rendering. I can fetch and update data from from forms but i just want to validate the fields. But i can't move forward as i didn't use any Django form models.Client side validation is going on but what about the Server side validation.
使用python v3和Django v2我没有使用过forms模型,它没有继承自forms.py.那我该如何验证?我的临时文件进行注册.`
Using python v3 and Django v2 I didn't used forms model doesn't inherited from the forms.py . So how can i validate?? my tempalate file for signup. `
<form action="{% url 'register' %}" method="post">
{% csrf_token %}
{% for message in messages %}
<div class="alert {% if message.tags %} alert-{{ message.tags }}{% endif %}">{{ message|safe }}</div>
{% endfor %}
<h3>Registration Form</h3>
<div class="form-group">
<input type="text" name="first_name" placeholder="First Name" class="form-control" required>
<input type="text" name="last_name" placeholder="Last Name" class="form-control" required>
</div>
<div class="form-wrapper">
<input type="text" name="email" placeholder="Email Address" class="form-control" required>
<i class="zmdi zmdi-email"></i>
</div>
<div class="form-wrapper">
<input type="text" name="phone" placeholder="Phone" class="form-control" required>
<i class="zmdi zmdi-phone"></i>
</div>
<div class="form-wrapper">
<input type="password" name="password1" placeholder="Password" class="form-control" required>
<i class="zmdi zmdi-lock"></i>
</div>
<div class="form-wrapper">
<input type="password" name="password2" placeholder="Confirm Password" class="form-control"
required>
<i class="zmdi zmdi-lock"></i>
</div>
<button>Register
<i class="zmdi zmdi-arrow-right"></i>
</button>
</form>
</div>
</div>
`views.py
def register(request):
if request.method == "POST":
first_name = request.POST['first_name']
last_name = request.POST['last_name']
phone = request.POST['phone']
email = request.POST['email']
password1 = request.POST['password1']
password2 = request.POST['password2']
if password1 == password2:
if User.objects.filter(phone=phone).exists():
messages.info(request, 'Requested phone exists')
elif User.objects.filter(email=email).exists():
messages.info(request, 'Requested email exists')
return redirect('register')
else:
user = User.objects.create_complete_user(first_name=first_name, last_name=last_name, phone=phone,
email=email, password=password1)
user.save()
messages.info(request, 'successfully user object is created')
return redirect('login')
else:
messages.info(request, 'Passwords not matching')
return redirect('register')
else:
return render(request, 'signup.html')
推荐答案
我唯一想到的将表单与Django分开添加的情况是,当前端和后端分开并且它们通过API进行通信时.在这种情况下,应使用序列化程序进行验证.此处有更多详细信息: https://www.django-rest-framework.org/api-guide/serializers/
The only situation I can think of for adding your forms separately from Django is when your front-end and back-end are split apart and they communicate though an API. If this is the case, you should use serializers for validation. More details here: https://www.django-rest-framework.org/api-guide/serializers/
如果您在Django中有特殊情况,但仍想使用编码的HTML表单,则还需要创建一个Django表单,以反映您在HTML中制作的表单.可以说您具有以下HTML输入类型:
If you have a special situation in Django and you still want to use your coded HTML form, you will also need to create a Django form that mirrors the form you made in HTML. Lets say you have the following HTML input types:
<form>
<input type="email" name="my_email">
<input type="text" name="my_text">
<input type="file" name="my_file">
</form>
您在Django中的表单必须相同
Your form in django must be identical
#forms
class Myform(forms.Form):
my_email = forms.EmailField()
my_text = forms.CharField()
my_file = forms.FileField()
# view
def myview(request):
myform = Myform(request.POST, request.FILES)
if myform.is_valid():
# Cleaned and validated data
my_email = myform.cleaned_data["my_email"]
my_text = myform.cleaned_data["my_text"]
my_file = myform.cleaned_data["my_file"]
# Do here whatever you need to do with
永远不要保存未经验证和清除的数据.
Never save data that has not been validated and cleaned.
# Never do this.
def myview(request):
my_email = request.POST["my_email"]
request.user.email = my_email
request.user.save()
注意:这是未经测试的代码,可能无法正常工作.但这只是您应该做什么的一个示例.
Note: This is untested code and might not work as is. But it serves as an example of what you should do.
这篇关于如何在不使用Django表单的情况下验证Django中的模板版本化表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!