What you can do is generate a random string and pass it to the password input in your form like this:
views.py
import random
import string
def register_new_user(request):
letters = string.ascii_lowercase
password = ''.join(random.choice(letters) for i in range(10))
if request.method == 'POST':
form = SignupForm(request.POST) # your signup form class
if form.is_valid():
user.save()
return redirect('home') # redirect to a valid page
else:
form = SignupForm()
return render(request, 'register_new_user.html' {'form': form,
'password': password})
Then in your template pass the password inside your password input field value attribute:
register_new_user.html
<form method="post">
{% csrf_token %}
<input type="text" name="username">
<input type="password" name="password1" value="{{ password }}">
<input type="password" name="password2" value="{{ password }}">
<button type="submit">Submit</button>
</form>
This way each time a user accesses your register_new_user
view the password fields will be prepopulated with random string.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…