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 file. Here, we list the URL’s the user will access and the view they point to in our Let’s take an example. We have the path ‘password’, this provides the rulset for any requests where a user types

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('', views.home, name='home'),
    path('password', views.password, name='password'),

Here, we have our 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'):

    if request.GET.get('special'):
    if request.GET.get('numbers'):

    while i < length:
        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
        <input type="checkbox" name="uppercase">uppercase </input>
        <input type="checkbox" name = "numbers">numbers </input>
        <input type="checkbox" name = "special">special </input>
    <input type="submit" value="Generate">

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 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.