[英]Django: Prevent form from resetting when a form is not valid
I am trying to prevent the form from resetting when there is an invalid input, but when there is an invalid input my whole form is resetting and I don't understand why.当输入无效时,我试图阻止表单重置,但是当输入无效时,我的整个表单正在重置,我不明白为什么。 This is basically a registration form and the point is to not have the form reset when form.is_valid() is false.这基本上是一个注册表单,重点是当 form.is_valid() 为 false 时不要重置表单。 Also, if you could help with displaying messages.另外,如果您可以帮助显示消息。 How do I incorporate a strength password bar under the password field.如何在密码字段下合并强度密码栏。
<center><div class= 'form-container'>
<h1 style="font-weight:normal;font-family: 'Google Sans','Noto Sans Myanmar UI',arial,sans-serif;position: relative;top: 20px;">Sign-up</h1>
<form method="post" id = "register-inputs" action = {% url 'register' %}>
{% csrf_token %}
<div>
<div class="user-register">
<input type = 'text' name = 'first_name' required>
<span class="highlight"></span>
<span class="bar"></span>
<label>First name</label>
</div>
<div class="user-register">
<input type = 'text' name = 'last_name' required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Last name</label>
</div>
<div class="user-register">
<input type = 'text' name = 'email' required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Email</label>
</div>
<div class="user-register">
<input type = 'text' name = 'username' required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Username</label>
</div>
<div class="user-register">
<input type = 'password' name="password1" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Password</label>
</div>
<div class="user-register">
<input type = 'password' name="password2" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Confirm Password</label>
</div>
<input class="btn btn-primary" type="submit" value="Sign up!">
<div class="social-media-btns">
<div class="or-login"></div>
<div class="social-icons">
<a href = "https://www.instagram.com" target="_blank" class="social-icon social-icon--instagram">
<i class="fa fa-instagram"><img width="40" style="display: flex; justify-content:center" src="{% static 'social_media_btn/instagram-icon.png' %}"></i>
<div class="tooltip">Instagram</div>
</a>
<a target="_blank" href="https://www.facebook.com" class="social-icon social-icon--facebook">
<i class="fa fa-facebook">
<img width="15" style="display: flex; justify-content:center" src = "{% static 'social_media_btn/facebook-icon.png' %}">
</i>
<div class="tooltip">Facebook</div>
</a>
<a target="_blank" href="https://www.google.com" class="social-icon social-icon--google">
<i class="fa fa-google">
<img width="40" style="display: flex; justify-content:center" src="{% static 'social_media_btn/google-icon.png' %}">
</i>
<div class="tooltip">Google</div>
</a>
</div>
</div>
<br>
<p>Already have an account? <span><a href="{% url 'login' %}">Login!</a></span>
</p>
</div>
</form>
</div></center>
views.py视图.py
def register(request):
form = RegistrationForm()
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = form.save()
user.refresh_from_db()
user.profile.first_name = form.cleaned_data.get('first_name')
user.profile.last_name = form.cleaned_data.get('last_name')
user.profile.email = form.cleaned_data.get('email')
user.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect('register')
else:
form = RegistrationForm()
context = {'form': form}
return render(request, 'register.html', context)
You can't see the values when the form is invalid because you have a custom HTML form (not Django rendered) and the value attributes aren't bound to the form fields.当表单无效时,您看不到值,因为您有自定义 HTML 表单(未呈现 Django)并且值属性未绑定到表单字段。 So do the following:因此,请执行以下操作:
<div class="user-register">
{{ form.first_name }}
<span class="highlight"></span>
<span class="bar"></span>
<label>First name</label>
</div>
OR或者
<div class="user-register">
<input type = 'text' name = 'first_name' required value="{{ form.first_name.value }}">
<span class="highlight"></span>
<span class="bar"></span>
<label>First name</label>
</div>
But I personally recommend the first one.但我个人推荐第一个。
UPDATE To style your fields within Django, use widgets.更新要在 Django 中设置字段样式,请使用小部件。 So for example, for the first_name
field, do the following:例如,对于first_name
字段,请执行以下操作:
forms.py
class MyForm(forms.Form):
first_name = forms.CharField(
widget=forms.TextInput(
attrs={"class": "custom-css-classes go-here"}
)
)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.