Making a random password generator website with django

While learning Django, it’s important to have some real world problems to tackle. Strong passwords are of course really important and so generators have become popular.

So, the first thing we do is edit our urls.py file. Here, we list the URL’s the user will access and the view they point to in our views.py. Let’s take an example. We have the path ‘password’, this provides the rulset for any requests where a user types http://www.example.com/password.

In this ruleset, we send them to views.pasword, which you’ll see below. We then give this an alias. In this case, it is also password. But you could have a path of /my_password and the alias could still be password. It’s used for referencing the URL – so the URL can change, the alias remains the same and the link will still work. Note the {% url ‘password’ %} piece of the homepage code below, showing how this is implemented.

from django.contrib import admin
from django.urls import path
from kodey import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('password', views.password, name='password'),
]

Here, we have our views.py. We have the home view defined, where we simply render the template of home.html.

We then have our password view. Here, we define the length of the required password, which is derived from the form field on the homepage.We then define a list of characters to be included in the password. Depending on the optioins the user has selected on the homepage, we will add uppercase, numeric and special characters to the list of possible characters to use for the password.

We then loop through and pick a character randomly from the list for each character in the the password. We then, pass that request back in a dictionary., which will then be displayed on the password.html page.

from django.shortcuts import render
from django.http import HttpResponse
import random

# Create your views here
def home(request):
    return render(request, 'kodey/home.html') 

def password(request):
    thepass = ""
    length = request.GET.get('length')
    length = int(length)
    characters = list('abcdefghijklmnopqrstuvwxyz')
    
    if request.GET.get('uppercase'):
        characters.extend(list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'))

    if request.GET.get('special'):
        characters.extend(list('£$%^&*!{}()'))
    
    if request.GET.get('numbers'):
        characters.extend(list('0123456789'))

    i=0
    while i < length:
        i=i+1
        thepass = thepass + random.choice(characters)
    return render(request, 'kodey/password.html', {'password': thepass})

The code for the home.html is shown below. Here, we have a simple for which the action is the URL of the passwords page, which as you remember above, is handled by the password view, hence the form values are ingested and a password is output.

        <h1> Password Generator </h1>
        <form action="{% url 'password'  %}">
        <select name="length">
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="30">30</option>
        </select> Password Length
        <br><br>
        <input type="checkbox" name="uppercase">uppercase </input>
        <br>
        <input type="checkbox" name = "numbers">numbers </input>
        <br>
        <input type="checkbox" name = "special">special </input>
        <br>
        <hr>
    <input type="submit" value="Generate">
</form>

The password.html page is configured as below. We use the value that has been passed in the view by using {{ password }} on the page. We then, again, use the alias from the urls.py file to make a link back to the homepage.

Your password is: 
{{ password }}

<a href="{% url 'home' %}">BACK</a>

This is a super simple example and has absolutely no formatting so it does look quite ugly, but it’s fully functional and is a good starting point for Django.