<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: stue</title>
    <description>The latest articles on DEV Community by stue (@stuartelimu).</description>
    <link>https://dev.to/stuartelimu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F99329%2F2446f64a-88c6-490c-afee-1fb06282d35f.jpg</url>
      <title>DEV Community: stue</title>
      <link>https://dev.to/stuartelimu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stuartelimu"/>
    <language>en</language>
    <item>
      <title>Create an authentication app for almost any django project.</title>
      <dc:creator>stue</dc:creator>
      <pubDate>Sun, 15 Sep 2019 09:19:24 +0000</pubDate>
      <link>https://dev.to/stuartelimu/create-an-authentication-app-for-almost-any-django-project-9kc</link>
      <guid>https://dev.to/stuartelimu/create-an-authentication-app-for-almost-any-django-project-9kc</guid>
      <description>&lt;p&gt;About a week ago I received the &lt;strong&gt;One Year Club&lt;/strong&gt; badge from &lt;a href="https://dev.to/"&gt;dev.to&lt;/a&gt; . I've been a registered member for a year now. &lt;/p&gt;

&lt;p&gt;I've not written any articles yet but I guess it's now time to give back to community because I've read so many  articles that have really helped me through my dev journey. &lt;/p&gt;

&lt;p&gt;I'll be starting a django series where I go through the process of creating an accounts app to manage user authentication for most of the projects you will create using django.&lt;/p&gt;

&lt;p&gt;This is the first part we will talk about, project setup, creating accounts app and then finally login.&lt;/p&gt;

&lt;p&gt;Prerequisites&lt;br&gt;
This is somewhat a beginner series but I expect you to know some basic django and Python. &lt;/p&gt;

&lt;p&gt;Let's get started already😊&lt;/p&gt;

&lt;p&gt;Open your terminal and change into the directory where you want your project to reside in. Create a new directory/folder and call it &lt;strong&gt;ausers&lt;/strong&gt; or any name you want to use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir ausers
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Change into this directory and then create a virtual environment where we will install django and other packages&lt;/p&gt;

&lt;p&gt;We will create a virtual environment called env. Once it has been created we will activate our virtual environment&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;virtualenv env

source env/bin/activate

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We will then install django using pip.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install django
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Once that has been installed, we will create a django project called &lt;strong&gt;ausers&lt;/strong&gt; this is the same name we gave to our directory earlier so at the end of the command we add a period to tell django to create the files in the current directory and no create a new one. If you want a different name for your project you can go ahead and specify the name instead of the period.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;django-admin startproject ausers .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You can now open the directory in your favorite text editor.&lt;/p&gt;

&lt;p&gt;We are now going to create &lt;strong&gt;accounts&lt;/strong&gt; app which will be used for user authentication&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./manage.py startapp accounts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Open settings.py and Register the &lt;strong&gt;accounts&lt;/strong&gt; app, take not it is at the top of all other apps&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Django comes with a built in authentication framework to handle user authentication, session , permissions and groups.&lt;/p&gt;

&lt;p&gt;Django comes with several class based views to handle authentication. We will deal with only two in this article&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LoginView : handles login form and logs in a user&lt;/li&gt;
&lt;li&gt;LogoutView : logs out a user &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All we need right now is to add these views to our urls.py file&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;accounts&lt;/strong&gt; app/directory, create a new file called urls.py and add these few lines on code&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We will now edit the urls.py file in the ausers directory to include the urls of the accounts app&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Okay we have done most of the part , let's do a quick check if our project is running.&lt;/p&gt;

&lt;p&gt;First, we make migrations then start the server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./manage.py migrate &amp;amp;&amp;amp; ./manage.py runserver
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you open this address &lt;code&gt;http://127.0.0.1:8000/&lt;/code&gt; in your browser you will get &lt;code&gt;Page Not Found(404)&lt;/code&gt; error. It's ok no need to freak out we got this.&lt;/p&gt;

&lt;p&gt;So we are receiving that error because we do not have any urls pointing to that address, we will come back to that.&lt;/p&gt;

&lt;p&gt;Lets create templates for the views we included in our urls file earlier.&lt;/p&gt;

&lt;p&gt;In the accounts folder create this directory &lt;code&gt;templates/registration&lt;/code&gt; and &lt;code&gt;templates/registration&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;In the same directory create this directory &lt;code&gt;static/accounts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Your folder structure should look like this&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2YudFyR7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/gcmtq66v1y1zfhw42l04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2YudFyR7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/gcmtq66v1y1zfhw42l04.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For simplicity I'm going to use this &lt;a href="https://colorlib.com/wp/template/login-form-v18/"&gt;login&lt;/a&gt; template from &lt;a href="https://colorlib.com/"&gt;colorlib&lt;/a&gt;. Go ahead and download it to your computer.&lt;/p&gt;

&lt;p&gt;Extract the files and open the folder. We will move all the static files(images, css, vendor, js) to the &lt;code&gt;static/accounts&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;We will create a base.html file in the &lt;code&gt;templates/accounts directory&lt;/code&gt;. You can have a look at the file &lt;a href="https://gist.github.com/stuartelimu/eeccebafa7b9c5df7a96bcea4a468cc5#file-base-html"&gt;here&lt;/a&gt;&lt;br&gt;
Basically we get all the common parts of the index.html page from the folder we downloaded and add it to the base.html file&lt;/p&gt;

&lt;p&gt;let's create the &lt;a href="https://gist.github.com/stuartelimu/eeccebafa7b9c5df7a96bcea4a468cc5#file-login-html"&gt;login.html&lt;/a&gt; template in &lt;code&gt;templates/registration&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The login form uses the default &lt;code&gt;AuthenticationForm&lt;/code&gt; which is located at &lt;code&gt;django.contrib.auth.forms&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This forms tries to authenticate users and raises a validation if login was unsuccessful&lt;/p&gt;

&lt;p&gt;You notice we added a hidden input field with a variable called &lt;code&gt;next&lt;/code&gt;. This parameter has to be a url . If this parameter is given django login view will redirect the user to the given url if login is successful.&lt;/p&gt;

&lt;p&gt;let's create the &lt;a href="https://gist.github.com/stuartelimu/eeccebafa7b9c5df7a96bcea4a468cc5#file-logged_out-html"&gt;logged_out.html&lt;/a&gt; template &lt;code&gt;templates/registration&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Django will display this template when a user has logged out.&lt;/p&gt;

&lt;p&gt;We are going to create a new view to display a template when a user logs in.&lt;/p&gt;

&lt;p&gt;Open views.py in the &lt;strong&gt;accounts&lt;/strong&gt; app.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We use the &lt;code&gt;LoginRequiredMixin&lt;/code&gt; which will redirect users to the login page if they are not authenticated. &lt;/p&gt;

&lt;p&gt;Add this url to the urls.py file in the accounts directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;path('', views.DashboardView.as_view(), name='dashboard'),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a &lt;a href="https://gist.github.com/stuartelimu/eeccebafa7b9c5df7a96bcea4a468cc5#file-dashboard-html"&gt;dashboard.html&lt;/a&gt; template in &lt;code&gt;templates/accounts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add the following to your settings file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOGIN_URL = 'login'

LOGOUT_URL = 'logout'

LOGIN_REDIRECT_URL = 'dashboard'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We are using the url names instead of the relative paths.&lt;/p&gt;

&lt;p&gt;Once that is all set we are going to create a superuser to test our login&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./manage.py createsuperuser
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Fill in your details and then start the server. &lt;/p&gt;

&lt;p&gt;Open your browser and go to this url &lt;br&gt;
&lt;code&gt;http://127.0.0.1:8000/accounts/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you're not logged in, you will be redirected to the login page. And that's it mainly for this article.&lt;/p&gt;

&lt;p&gt;Well done, phew that was something. Let's have a recup&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;used django's built in classes to add authentication&lt;/li&gt;
&lt;li&gt;created custom templates for our views&lt;/li&gt;
&lt;li&gt;configured settings &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Congratulations, you now have a working login. We will be building more on this so keep follow me so you don't miss out.&lt;/p&gt;

&lt;p&gt;Feel free to reach out to me if anything didn't make sense or if you found an easier way. And if you found any mistake feel free to let me know in the comments.&lt;/p&gt;

&lt;p&gt;Finally, this is my first article here but I have a couple on my &lt;a href="https://www.linkedin.com/in/stuartelimu/"&gt;linkedIn&lt;/a&gt;. Feel free to connect with me.&lt;/p&gt;

</description>
      <category>django</category>
    </item>
  </channel>
</rss>
