如何显示Django模板上的对象列表?

问题描述 投票:0回答:1

我发现的所有答案均不适用于我的问题。

我正在尝试显示用户在模板“ registro_denuncias”中发布的注册,但是当我尝试列出对象时,什么也没有显示。我也检查了数据库,并且有测试寄存器,因此寄存器被保存但未显示。

这些是我的代码:

models.py

from django.db import models
from django.utils import timezone

class Post(models.Model):
    tuNombre = models.CharField(max_length=100)
    tuApellidos = models.CharField(max_length=100)
    tuRut = models.CharField(max_length=30)
    edad = models.IntegerField()
    fecha_publicacion = models.DateTimeField(
        default=timezone.now)
    fecha_suceso = models.DateField(default='DD/MM/AAAA',
            blank=True, null=True)
    nombresAgresor = models.CharField(max_length=100)
    apellidosAgresor = models.CharField(max_length=100)
    rutAgresor = models.CharField(max_length=30)
    lugarSuceso = models.CharField(default='',max_length=50)
    descripcionSuceso = models.TextField()

    def publish(self):
        self.published_date = timezone.now()
        self.save()
    def __str__(self):
        return self.tuRut

forms.py

from django import forms
from django.forms import ModelForm
from .models import Post

class PostForm(ModelForm):
    class Meta:
        model = Post
        fields=[
            'tuNombre', 
            'tuApellidos',
            'tuRut', 
            'edad', 
            'fecha_publicacion', 
            'fecha_suceso', 
            'nombresAgresor', 
            'apellidosAgresor', 
            'rutAgresor',
            'lugarSuceso', 
            'descripcionSuceso'
            ]
        labels = {
            'tuNombre':'Tus Nombres', 
            'tuApellidos':'Tus Apellidos',
            'tuRut':'Tu Rut (Ej: 11222333-4)', 
            'edad':'Tu Edad', 
            'fecha_publicacion':'Fecha de Publicación (Hoy)', 
            'fecha_suceso':'Fecha del Suceso', 
            'nombresAbusador':'Nombres de tu Abusador', 
            'apellidosAgresor':'Apellidos de tu Agresor', 
            'rutAgresor':'Rut de tu Agresor (Ej: 11222333-4)', 
            'lugarSuceso':'Ciudad del Suceso',
            'descripcionSuceso':'Descripción del Suceso'
        }

views.py

from django.shortcuts import render, redirect
from django.views.generic.edit import FormView, View
from django.views import generic
from django.views.generic.list import ListView
from .models import Post
from .forms import PostForm

def index(request):
    return render(request, 'denuncias/index.html', {})
def registro_denuncias(request):
    denuncias = Post.objects.all()
    data={
        'denuncias': denuncias
    }
    return render(request,'denuncias/registro_denuncias.html', {}) 

def add(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.save()
            return redirect('/add')
    else:
        form = PostForm()
        return render(request, 'denuncias/add.html',
                      {'form': form})

urls.py

from django.contrib import admin
from django.urls import path, include
from funas import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('funas.urls')),
    path('add/', include('funas.urls')),
    path('registro_denuncias/', include('funas.urls')),
]

和...

from django.urls import path, include
from . import views, forms
from .views import *

urlpatterns = [
    path('', views.index),
    path('add/', views.add, name="add"),
    path('registro_denuncias/', views.registro_denuncias),
]

最后是我的template

{% extends 'denuncias/base.html'%}

    {% block contenido %}
    <h1>Listado Denuncias</h1>
    <table class="table table-bordered">
        <thead> 
            <tr>
                <td>Nombres</td>
                <td>Apellidos</td>
                <td>Rut</td>
                <td>Suceso</td>
                <td>Nombre Víctima</td>
            </tr>
        </thead>
        <tbody>

            {% for d in denuncias %}
            <tr>
                <td>{{ d.nombresAgresor }}</td>
                <td>{{ d.apellidosAgresor }}</td>
                <td>{{ d.rutAgresor }}</td>
                <td>{{ d.descripcionSuceso }}</td>
                <td>{{ d.tusNombre }} {{ d.tusApellidos }}</td>
            </tr>
            {% endfor %}

        </tbody>
    </table>    
    {% endblock %}

¿我想念什么?帮助。

python html django list listview
1个回答
0
投票

您需要在视图中将data传递给render

    return render(request,'denuncias/registro_denuncias.html', data)
© www.soinside.com 2019 - 2024. All rights reserved.