Remember the https://coinmarketcap.com/ clone we did in our previous class? ๐ช Today we will augment it with some authentication capabilities. ๐
We want users to be able to log in and log out from the app, using the built-in Django authentication views. Check the official documentation for extra details: https://docs.djangoproject.com/en/2.0/topics/auth/default/
There's no need to implement the log in and log out features, Django comes with it out of the box. Just set the proper URLs, and add the link in your navbar like this:
The view will look by default for a template called templates/registration/login.html
. Just create a template with that name, and give it the styles you want. It should look something like this:
For the log out view, you won't need any template. Just point the link to the URL you configured in your utls.py
module, and that's it.
As a second step, we want the users of our app to be able to create new Cryptocurrencies. For that we need to create a custom Django ModelForm, which will handle all the fields validation for us, according to the rules configured in the Cryptocurrency
model.
Add a button in the main page, with a link to /create
. Should look something like:
In the /create
template, we will render a form with only a few of the required models to create a Cryptocurrency object. Check the following screenshot for extra details:
The ModelForm will handle all the fields validation, but you will need to show errors in the template properly if they are present.
hint: check the django-bootstrap4
library for easy integration of Bootstramp v4 in templates
We want to add a new FavouriteCoin
model. It will have two new ForeignKey
fields, one pointing to User
and one to Cryptocurrency
. Once you have the code in models.py
, make sure to create the needed migrations.
If you are done, open the Django Shel (run django-admin shell
command), and play with the new model, creating or fetching them from the DB.
That's all! ๐ We just did a second iteration to the Coinmarketcap clone, adding some more advanced functionalities of the Django framework.