How to show data in the template

Asked

Viewed 42 times

0

I have this model:

class template(models.Model):

    nome = models.CharField(max_length=30)

is view:

def home(request):
    dados = template.objects.all()
    return render(request,'index.html', {'dados':dados})

template:

<!DOCTYPE html>
{% load static %}
<html lang="pt-br">


    <head>
      <meta charset="utf-8">
      <title> {{ template.nome }} </title>
      <meta content="width=device-width, initial-scale=1.0" name="viewport">
      <meta content="" name="keywords">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">  <!-- Favicons -->
      <link href="{% static 'img/favicon.png' %}" rel="icon">
      <link href="{% static 'img/apple-touch-icon.png' %}" rel="apple-touch-icon">

      <!-- Google Fonts -->
      <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,700,700i|Roboto:100,300,400,500,700|Philosopher:400,400i,700,700i" rel="stylesheet">

      <!-- Bootstrap css -->
      <!-- <link rel="stylesheet" href="css/bootstrap.css"> -->
      <link href="{% static 'lib/bootstrap/css/bootstrap.min.css" rel="stylesheet' %}">

      <!-- Libraries CSS Files -->
      <link href="{% static 'lib/owlcarousel/assets/owl.carousel.min.css' %}" rel="stylesheet">
      <link href="{% static 'lib/owlcarousel/assets/owl.theme.default.min.css' %}" rel="stylesheet">
      <link href="{% static 'lib/font-awesome/css/font-awesome.min.css' %}" rel="stylesheet">
      <link href="{% static 'lib/animate/animate.min.css' %}" rel="stylesheet">
      <link href="{% static 'lib/modal-video/css/modal-video.min.css' %}" rel="stylesheet">

      <!-- Main Stylesheet File -->
      <link href="{% static 'css/style.css' %}" rel="stylesheet">


    </head>

And I’m calling the template a: {{ template.name }}

My intention is to create a form so that the user can edit data in the template (eg: company name, contacts, slogan), I am using Django admin for the form but nothing appears in the template, can anyone point out the error? this is the right way to do it?

  • Da para vc colocar o codigo da sua template?

  • Ready put.

  • Okay, but where are you showing the data in the template? I don’t understand what you want.

1 answer

0


In your example you are not treating the data that was sent to the template, based on your code I will create an example, but, to simplify, my data will be "hardcoded" instead of coming from a queryset:

View file.py

def home(request):
    dados = dict(Nome = 'Danilo Costa', Fone='123456')
    return render(request,'index.html', dados)

Let’s say in the template you want to present the name and address sent through the dictionary dados:

index.html file

<p>
    O Nome enviado foi {{dado.nome}}<br>
    O endereço enviado foi {{dado.endereço}}
</p>

Browser other questions tagged

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