User AuthenticationIn this section I will show an example about how to create sign-in form, and after this how to use a basic sign-up/log-in logic in your web application.
The prerequisit of the next task is an already done custom user model. We should also have a superuser account, with which we are able to log in. As superuser, we can add new users setting for them different permissions on the admin interface, but it is more convenient for our prospective users to sign up and log in by themselves.
Go to your project folder!
Add accounts app to the '#Local apps' right under the '#User model'!
Go to your accounts app folder!
Update views.py!
Update urls.py!
Create signup.html template in your project folder!
If you have installed crispy-form before, by now you are able to apply as a template. Applying crispy form we could gain more fancy looks of the input fields, buttons and so on.
You'd notice expressions between curly brackets and percentage symbols. This is the so called Django template language. You can learn more about this from the documentation.
After our 'sign up' model and form have done, it is time to create the 'sign in' things as well.
Add 'home' as redirection destiny!
In this case both login and logut is pointing to the same destination. Content availability will be the difference. You can see later in this section!
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'
Create login form with crispy!
Apply the content availability for the logged in user!
If I am logged in then I am able to see a greeting and my email address signed up with earlier. And if I am not logged in then I will get a message about that I am really not.
Other way. Use decorator!
An other solution to make 'logging in' being required for any of your page just use decorator.
To apply the next view do not forget adding 'examplePageView' to your app's urls.py file! Like this.