Create user and show all user rows in Django template

Posted under » Django updated on on 2 December 2025

In order to create a new user, you need to import auth user models and do this.

from django.contrib.auth.models import User
from django.http import HttpResponse

def baru(request):
    user = User.objects.create_user('weiling','amdead@lky.com','weiling@pswrd')
    user.last_name = "LTI"
    user.save()
    return HttpResponse("created new user")

If you need to know the id of the user that you have created,

def vserid(request):
    user = User.objects.get(username='weiling')
    user_id = user.id
    name = user.username
    return HttpResponse(f"Username : {name} <p>user id : {user_id}")

More on creating a record in Django. Please also read the introductory tutorial on template. For basic array article. In Django, HttpResponse ia not the equivalent to print() To iterate list in Django, you have to use a template.

This example use a database or model. The views.py

def index(request):
    notes = Tier1.objects.all().values()
    if notes.exists(): 
            context = {'tier1': notes}
            return render(request, 'notes/contentsummary.html', context)
    else :
        return HttpResponse("You're haz no success")

The values() part is optional. Now lets look at the template

{% if tier1 %}

Tier 1 Topics

{% else %}

No notes are available

{% endif %}

Normally we want to show details from a single row.

There may be time where you want to show all the users in Django. To do this, you need to import auth user models by

from django.contrib.auth.models import User
def email(request):
    #users = User.objects.all()
    email_list = list(User.objects.values_list("email", flat=True))
    return render(request, 'nafi/email.html', {'my_list': email_list})

Template may look like this

<ul>
        {% for email in my_list %}
            <li>{{ email }}</li>
        {% endfor %}
</ul>

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data