46
loading...
This website collects cookies to deliver better user experience
Thanks for reading! - Content provided by App Generator.
Topics covered in this tutorial
User
model can be found below:$ git clone https://github.com/app-generator/django-learn-by-coding.git
$ cd django-learn-by-coding
Create a virtual environment - Linux-based systems
$ virtualenv env
$ source env/bin/activate
For Windows system, the syntax is different:
$ virtualenv env
$ .\env\Scripts\activate
Install Django
$ pip install django
superuser
that allows us to access the admin section. For newcomers, the admin
section manages the registered users, groups defined in our project.$ # We are in the ROOT of the project
$ python manage.py createsuperuser
sername (leave blank to use 'test'): admin
Email address: [email protected]
Password: ********
Password (again): ********
Superuser created successfully.
superuser
admin is created we can access the admin
section and interact with all models registered by our project. Let's explore the users using the Django CLI:>>> from django.contrib.auth.models import User
>>> User.objects.all()
<QuerySet [<User: admin>]>
Create a new (common) user
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('test', '[email protected]', 'Super_S3cret111')
>>> >>> User.objects.all()
<QuerySet [<User: admin>, <User: test>]>
Create the SignUp Form
class SignUpForm(UserCreationForm):
username = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder" : "Username"
}
))
email = forms.EmailField(
widget=forms.EmailInput(
attrs={
"placeholder" : "Email"
}
))
password1 = forms.CharField(
widget=forms.PasswordInput(
attrs={
"placeholder" : "Password"
}
))
password2 = forms.CharField(
widget=forms.PasswordInput(
attrs={
"placeholder" : "Password check"
}
))
Create the controller
def register_user(request):
# A user-friendly message
msg = None
# User submits the credentials
if request.method == "POST":
# Initialize the from POST data
form = SignUpForm(request.POST)
# Check all constraints (one line)
if form.is_valid():
# Create the user
form.save()
msg = 'User created successfully.'
else:
msg = 'Form is not valid'
# Show the SignUp Page
else:
form = SignUpForm()
return render(request, "accounts/register.html", {"form": form, "msg" : msg })
The page that shows the form and invite the user to register
<form role="form" method="post" action="">
{% csrf_token %}
<div>
{{ form.username }}
</div>
<span class="text-error">{{ form.username.errors }}</span>
<div>
{{ form.email }}
</div>
<span class="text-error">{{ form.email.errors }}</span>
<div>
{{ form.password1 }}
</div>
<span class="text-error">{{ form.password1.errors }}</span>
<div>
{{ form.password2 }}
</div>
<span class="text-error">{{ form.password2.errors }}</span>
<button type="submit" name="register">Register</button>
</form>
The user registration mechanism
create-user
method as well:def register_user(request):
# A user-friendly message
msg = None
# User submits the credentials
if request.method == "POST":
# Initialize the from POST data
form = SignUpForm(request.POST)
# Check all constraints (one line)
if form.is_valid():
username = form.cleaned_data.get("username") # <-- UPDATED
email = form.cleaned_data.get("email") # <-- UPDATED
raw_password = form.cleaned_data.get("password1") # <-- UPDATED
# Create user: UPDATED
new_user = User.objects.create_user(username, email, raw_password)
msg = 'User created successfully.'
else:
msg = 'Form is not valid'
# Show the SignUp Page
else:
form = SignUpForm()
return render(request, "accounts/register.html", {"form": form, "msg" : msg })
Thanks for reading! Feel free to AMA in the comments section. For more resources, please access: