Okay. This is my urls.py. It contains the paths for the register page where I create an account as well as the path for resetting the password.
urlpatterns = [
path('login/',views.loginPage, name="login"),
path('register/',views.registerPage, name="register"),
path('logout/',views.logoutUser, name="logout"),
path('dashboard/',views.dashboardPage, name="dashboard"),
path('account_settings/',views.accountSettings, name="accountSettings"),
path("reset_password/",
auth_views.PasswordResetView.as_view(template_name="accounts/password_reset.html"),
name="reset_password"),
path("reset_password_sent/",
auth_views.PasswordResetDoneView.as_view(template_name="accounts/password_reset_sent.html"),
name="password_reset_done"),
path("reset/<uidb64>/<token>/",
auth_views.PasswordResetConfirmView.as_view(template_name="accounts/password_reset_form.html"),
name="password_reset_confirm"),
path("reset_password_complete/",
auth_views.PasswordResetCompleteView.as_view(template_name="accounts/password_reset_done.html"),
name="password_reset_complete"),
]
This is the code for the form I use to create users in the register page.
class CreateUserForm(UserCreationForm):
#makes the password input optional
def __init__(self, *args, **kwargs):
super(UserCreationForm, self).__init__(*args, **kwargs)
self.fields['password1'].required = False
self.fields['password2'].required = False
# If one field gets autocompleted but not the other, our 'neither
# password or both password' validation will be triggered.
self.fields['password1'].widget.attrs['autocomplete'] = 'off'
self.fields['password2'].widget.attrs['autocomplete'] = 'off'
password1 = forms.CharField(widget = forms.PasswordInput(attrs ={'placeholder': 'Password'}))
password2 = forms.CharField(widget = forms.PasswordInput(attrs ={'placeholder': 'Please retype your password'}))
class Meta:
model=User
fields = ['username','email','password1','password2','first_name','last_name']
widgets = {
"username": forms.TextInput(attrs={'placeholder': 'Username'}),
"email": forms.EmailInput(attrs={'placeholder': 'Email'}),
"first_name": forms.TextInput(attrs={'placeholder': 'First Name'}),
"last_name": forms.TextInput(attrs={'placeholder': 'Last Name'}),
}
This is my register.html file. Here I create an account without setting the password. The password is randomly set in the views.py (which is coming up).
{% extends 'home/base.html' %}
{% load static %}
{% block content %}
<section id="register">
<div class="container">
<div class="register_box">
<div class="form_heading">
<h1>Register</h1>
</div>
<form action="" method="POST">
{% csrf_token %}
{% for non_field_error in form.non_field.errors %}
<p class="error_message">{{ non_field_error }}</p>
{% endfor %}
<div class="field_wrapper">
{{form.username}}
{% for error in form.username.errors %}
<p class="error_message">{{ error }}</p>
{% endfor %}
</div>
<div class="field_wrapper">
{{form.email}}
{% for error in form.email.errors %}
<p class="error_message">{{ error }}</p>
{% endfor %}
</div>
<!-- <div class="field_wrapper">
{{form.password1}}
{% for error in form.password1.errors %}
<p class="error_message">{{ error }}</p>
{% endfor %}
</div>
<div class="field_wrapper">
{{form.password2}}
{% for error in form.password1.errors %}
<p class="error_message">{{ error }}</p>
{% endfor %}
</div> -->
<div class="field_wrapper">
{{form.first_name}}
{% for error in form.first_name.errors %}
<p class="error_message">{{ error }}</p>
{% endfor %}
</div>
<div class="field_wrapper">
{{form.last_name}}
{% for error in form.last_name.errors %}
<p class="error_message">{{ error }}</p>
{% endfor %}
</div>
<button type="submit" class="success_btn"> Register </button>
<p> Already have an account? <a href="{% url 'login' %}" class="help_text"> Login Here</a> </p>
</form>
</div>
</div>
</section>
this is the function for the registering the user in the views.py.
@staff_member_required
def registerPage(request):
if not request.user.is_authenticated or request.user.username == 'admin_username':
form = CreateUserForm()
if request.method == "POST":
form=CreateUserForm(request.POST)
if form.is_valid():
user = form.save()
user.set_password(get_random_string())
user.save()
username = form.cleaned_data.get("username")
email = form.cleaned_data.get("email")
name = form.cleaned_data.get("first_name")
url = request.build_absolute_uri('reset/<uidb64>/<token>/')
set_password_form = PasswordResetForm({'email':email,'url':url , 'username':username,"name":name})
assert set_password_form.is_valid()
set_password_form.save(
request=request,
from_email="email",
email_template_name="accounts/welcome_email_template.html",
html_email_template_name="accounts/welcome_email_template.html",
)
messages.success(request,"Account was created for " + username )
return redirect("login")
context= {"form":form}
return render(request, 'accounts/register.html', context)
else:
return redirect('home')
And finally, this is my welcome_email_template.html which is passed to the passwordresetform and contains the link for the new user to reset their password.
Hello {{name}},
The team would like to officially welcome you on board as investor.
We are pleased to inform you that your online investment account is now active.
Please click on the link below, which will guide you into setting up your password. The details for your account are also listed below.
{{url}}
username : {{username}}
email : {{email}}
If you have any further questions or clarification on this matter, kindly reach out to us at random@gmail.com.
Happy Investing!
I believe that should be all the code relating to the automatic email functionality. If there is anything else you need, I will add it. And thank you very much for the support so far.