有沒有辦法讓我在第一次重定向后停止其他驗證
在views.py中
from django.contrib import messages
# from django.core.exceptions import ValidationError
from django.shortcuts import render, redirect
def register(request):
if request.method == 'POST':
form = request.POST
phone_number = validate_phone(request, form.get("phone_number"))
username = validate_user_name(request, form.get("username"))
password = validate_password(request, password1=form.get("password1"),password2=form.get("password2"))
....other_fields....
return render(request, 'registration.html',)
def validate_user_name(request, username):
username = username.strip()
new = MyUser.objects.filter(username=username)
if new.count():
messages.error(request, "User with that Username Already Exist")
return redirect("pre_register")
# raise ValidationError("User with that Username Already Exist", code="username_error")
return username
def validate_password(request, password1, password2):
if password1 and password2 and password1 != password2:
messages.error(request, "Passwords don't match")
return redirect("pre_register")
# raise ValidationError("Password don't match")
return password2
....other_validations....
在registration.html中:
<h3 class="register-heading">Apply as a Member</h3>
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message.tags }} : {{message}}</li>
{% endfor %}
<form method="post">
<input type="text" class="form-control" placeholder="Username *" value=""
required="required" name="username"/>
<input type="text" minlength="10" maxlength="10" name="phone_number"
class="form-control"
placeholder="Your Phone *" value="" required="required"/>
<input type="password" class="form-control" placeholder="Password *" value=""
required="required" , name="password1"/>
<input type="password" class="form-control" placeholder="Confirm Password *"
value="" required="required" , name="password2"/>
</form>
Let's say a user enters a username that already exists, what I want is:
- To keep the already entered post data
- Once it hits this line
messages.error(request, "User with that Username Already Exist")
return redirect("pre_register")
, I want just this as result(In template)
error: User with that Email ALready Exists
Instead, I get all the error messages at once
error: User with that Email ALready Exists
error: Passwords don't match
...all the other errors....
I know it's because technically, this is what I should get but how do I achieve what I want above. I think a way to do this would be to clear all the messages before I set the error messages but that would mean giving the server more work to do as it would still go through all of them before leaving just the last one (And the fact that I don't know how to even do it )
Any help on this would be appreciated immensely
uj5u.com熱心網友回復:
首先,我認為您對何時使用渲染和何時使用重定向感到困惑。通常的模式是redirect在您的表單驗證時使用,以便用戶不會意外地重新提交資料,并render在資料無效時使用,以便呈現錯誤。你在做相反的事情。有關更多詳細資訊,請參閱此反模式。
其次,問題是您的驗證函式回傳 HTTP 回應。用戶永遠不會在您的驗證功能中被重定向。您需要從視圖函式回傳 HTTP 回應以使用戶重定向。您可能可以使用帶注釋的方法來提高ValidationErrors然后在視圖中捕獲它們。如果發生錯誤,則添加一條訊息并呈現模板。
就像是:
def register(request):
if request.method == 'POST':
form = request.POST
try:
phone_number = validate_phone(request, form.get("phone_number"))
username = validate_user_name(request, form.get("username"))
password = validate_password(request, password1=form.get("password1"),password2=form.get("password2"))
....other_fields....
# If all validations pass, you redirect
return redirect('pre_register')
except ValidationError as e:
messages.error(request, str(e))
# passes down to render
# This gets called when it's GET request or the form validation failed.
return render(request, 'registration.html',)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/456150.html
