(Django) Need help getting things from the database

So, lets try this again, I very new to programing and don't understand much about the specifics so bear with me for a little, I am making a website that have a login and ticket system, I followed a tutorial and made the login system using Django authenticate and login

(Views.py is being shown in two parts as to be easier to understand, at least I hope so)

Views.py (Register part)

from django.contrib.auth import authenticate, login

#Registering system, requires that both passwords area equal to register
def cadastro(request):
    return render(request,'cadastro.html')

def store(request):
    data = {}
    if(request.POST['password'] != request.POST['password-conf']):
        data['msg'] = 'Senhas diferentes! Tente Novamente'
        data['class'] = 'alert-danger'
    else:
        user = User.objects.create_user(request.POST['user'], request.POST['email'], request.POST['password'])
        user.save()
        data['msg'] = 'Usuário cadastrado com sucesso!'
        data['class'] = 'alert-success'
    return render(request,'cadastro.html',data)

Cadastro.hmtl

<form name="form-users" id="formUsers" method="post" action="/store/">
    <div class="col-4 mt-4 m-auto">
        {% if msg %}
            <div class="alert {{class}}">
                {{msg}}
            </div>
        {% endif %}

        {% csrf_token %}
        <input class="form-control mt-4" type="text" name="user" id="user" placeholder="Nome de Usuario:">
        <input class="form-control mt-4" type="email" name="email" id="email" placeholder="Email:">
        <input class="form-control mt-4" type="password" name="password" id="password" placeholder="Senha:">
        <input class="form-control mt-4" type="password" name="password-conf" id="password-conf" placeholder="Repita a Senha:">
        <input class="btn btn-primary mt-4" type="submit" value="Cadastrar">
        <a href="/loginv">Ja tem uma conta? Faça Login</a>
    </div>
</form>

Views.py(Login part)


from django.contrib.auth import authenticate, login

#Login system
def loginv(request):
    return render(request,'loginv.html')

def loginr(request):
 data = {}
 user = authenticate(username=request.POST['user'], password=request.POST['password'])
 if user is not None:
        login(request, user)
        return redirect('/dashboard/')
 else:
        data['msg'] = 'Usuário ou Senha inválidos!'
        data['class'] = 'alert-danger'
        return render(request,'loginv.html',data)

loginv.html

<form name="form-login" id="formLogin" method="post" action="/loginr/">
    <div class="col-4 mt-4 m-auto">
        {% if msg %}
            <div class="alert {{class}}">
                {{msg}}
            </div>
        {% endif %}

        {% csrf_token %}
        <input class="form-control mt-4" type="text" name="user" id="user" placeholder="Nome de Usuario:">
        <input class="form-control mt-4" type="password" name="password" id="password" placeholder="Senha:">
        <input class="btn btn-primary mt-4" type="submit" value="Entrar">
        <a href="/cadastro">Ainda não tem uma conta? Cadastre-se</a>
    </div>
</form>

So, both of these are being used and all work fine, but I didn't declare them on models.py and now I don't know how to get them from the database, I guess the tables came automatically with Django or when I created the project but I really don't know, I would really appreciate if someone can explain me this, also since my post got closed by not giving enough info I will post the temporary ticket form and models.py just in case, they work just fine too, but I need the user name to actually do the ticket

ticket.html

{% block content %}
    {% if request.user.is_authenticated %}
    <form name="form-ticket" id="formTicket" method="POST" action="/ticketr/">
        <div class="col-4 mt-4 m-auto">
            {% if msg %}
                <div class="alert {{class}}">
                    {{msg}}
                </div>
            {% endif %}
    
            {% csrf_token %}
            <input class="form-control mt-4" type="text" name="numctt" id="numctt" placeholder="Numero para contato:">
            <input class="form-control mt-4" type="text" name="nomctt" id="nomctt" placeholder="Nome para contato:">
            <input class="form-control mt-4" type="int" name="areaserv" id="areaserv" placeholder="Area do servico:">
            <input class="form-control mt-4" type="int" name="tiposerv" id="tiposerv" placeholder="Tipo de servico:">
            <input class="form-control mt-4" type="text" name="tickettext" id="tickettext" placeholder="Descricao:">
            <input class="btn btn-primary mt-4" type="submit" value="Entrar">
            
        </div>
    </form>
    {% else %}
        Você não tem acesso a essa área!
    {% endif %}
{% endblock %}

models.py

from django.db import models

# Create your models here.

class Userdb(models.Model):
    ticketid = models.AutoField(primary_key=True)
    nomctt = models.CharField(max_length=50)
    numctt = models.CharField(max_length=9)
    tiposerv = models.IntegerField()
    areaserv = models.IntegerField()
    tickettext = models.CharField(max_length=200) 
    
    class Meta:
        db_table = 'dbuser'

I don't know how to get the data collected by Django authentication system

I guess the tables came automatically with Django or when I created the project but I really don't know

Yes exactly. Django ships with its own authentication system django.contrib.auth which is enabled by default (docs).

If you are using that, then you will want to query the models of that django.contrib.auth module like described here, for example:

from django.contrib.auth.models import User
print(User.objects.all())

Alternatively, you can use the get_user_model function to get a reference to the current User model:

from django.contrib.auth import get_user_model
user_model = get_user_model()
print(user_model.objects.all())

This is more robust, in case you ever decide to create your own User model.

UPDATE

Oh that helps a lot, its was what I am looking for, but how do I display this on HTML, like how do I display the user name

To get the username of a User object, use the username attribute: user_object.username. To be able to display the username in your template, you have to include it in the context that is available to the template. For example in your views, since you are using the render function, data is that template context. You can either add the user object or the username to the data:

def store(request):
    data = {}
    if(request.POST['password'] != request.POST['password-conf']):
        ...
    else:
        user = User.objects.create_user(request.POST['user'], request.POST['email'], request.POST['password'])
        user.save()
        data['msg'] = 'Usuário cadastrado com sucesso!'
        data['class'] = 'alert-success'
        data['user'] = user
        data['username'] = user.username
    return render(request,'cadastro.html',data)

Then you can use that in your template:

{% block content %}
    {% if request.user.is_authenticated %}
        <p>Hello, user '{{ user.username }}'</p> 
        <p>Hello, user '{{ username }}'</p> 
        ...

To easily get the currently logged in user, you can use user attribute of the request object. Include the request in your template context data['request'] = request and then you can just use that in the template:

<p>Hello, user '{{ request.user.username }}'</p>
Back to Top