display a single Django class value

Asked

Viewed 25 times

0

I would like to know how to show in my html template, a single character of my model "Character".

this is my models.py, I inserted 10 characters in this class via Django admin:

py models file.

from django.db import models

# Create your models here.
class Personagem(models.Model):
    Nome = models.CharField(max_length=255)
    Idade = models.IntegerField(blank=True, null=True)
    Espécie = models.CharField(max_length=255)
    Gênero = models.CharField(max_length=255)
    Ocupação = models.CharField(max_length=255)
    Descrição = models.TextField(max_length=999)

    def __str__(self):
        return self.Nome

there is a character whose field name is "Finn", wanted to display this value in my html, this is my template:

char_info.html file

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Adventure Time</title>
    {% include 'parciais/head.html' %}
</head>
<body>
    <h1>{{ personagens.Nome }} </h1>
    
</body>
</html>

wanted in this {{ characters. Name }} appeared the character’s name, but do not know how to do, is not returning anything of this my code.

my views.py

from django.shortcuts import render, get_object_or_404
from .models import Personagem


def index(request):
    personagens = Personagem.objects.all()
    return render(request, 'home/index.html', {
        'personagens': personagens
    })


def Finn(request):
    personagens = Personagem.objects.get(Nome=personagens.Nome)
    return render(request, 'home/char_info.html', {
        'personagens': personagens
    })

py file.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('Finn', views.Finn, name='Finn'),
]

unfortunately I did not find any such example on the net, only found using the loop {% for %}, and displaying all values, as I do to get a single value of a single field of the class.?

1 answer

1

Along those lines:

personagens = Personagem.objects.get(Nome=personagens.Nome)

Pass the character name as parameter this way:

personagens = Personagem.objects.get(Nome='Finn')

If you want the character search to be dynamic, you must specify in the parameter in the url. In urlpatterns replace path('Finn') for path(<str:nome>) and in your Finn function you add the name as parameter.

def Finn(request, nome):
    personagens = Personagem.objects.get(Nome=nome)
    return render(request, 'home/char_info.html', {
        'personagens': personagens
    })
  • did not work, now gives this error: Reverse for 'Finn' with no arguments not found. 1 pattern(s) tried: ['(?P<nome>[^/]+)$']

  • the first template is index.html, {% extends 'base.html'%}&#xA;{% block conteudo %}&#xA;{% load static %}&#xA;&#xA;<div class="content">&#xA; </br>&#xA; <h1 style="text-align: center;">Lista de Personagens</h1>&#xA; <ul class="chars">&#xA; <li class="finn">&#xA; <a href="{% url 'Finn' %}">&#xA; <figure>&#xA; <img src="{% static 'finn.png' %}">&#xA; <figcaption></figcaption>&#xA; </figure>&#xA; </a>&#xA; </li> the problem is this url: <a href="{% url 'Finn' %}"> ?

  • In fact only the dynamic search that didn’t work, when I just changed that line:personagens = Personagem.objects.get(Nome='Finn') searched the character’s name correctly, but this part of the dynamic search only this "Reverse for 'Finn' with no Arguments not found. 1 Pattern(s) tried: ['(? P<name>[^/]+)$']"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.