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.?
did not work, now gives this error:
Reverse for 'Finn' with no arguments not found. 1 pattern(s) tried: ['(?P<nome>[^/]+)$']
– Anderson Santana Nunes
the first template is index.html,
{% extends 'base.html'%}
{% block conteudo %}
{% load static %}

<div class="content">
 </br>
 <h1 style="text-align: center;">Lista de Personagens</h1>
 <ul class="chars">
 <li class="finn">
 <a href="{% url 'Finn' %}">
 <figure>
 <img src="{% static 'finn.png' %}">
 <figcaption></figcaption>
 </figure>
 </a>
 </li>
the problem is this url:<a href="{% url 'Finn' %}">
?– Anderson Santana Nunes
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>[^/]+)$']"– Anderson Santana Nunes