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

2024-09-30 16:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我找到的所有答案都与我的问题不符

我试图显示用户在模板“registro\u denuncias”中发布的寄存器,但是当我试图列出对象时,什么也没有显示。我还检查了数据库,测试寄存器也在那里,所以寄存器保存了,但没有显示

这些是我的密码:

型号.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

表单.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'
        }

视图.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})

url.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),
]

最后是我的模板

{% 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 %}

你知道吗¿我错过了什么?救命啊


Tags: pathdjangofromimportaddmodelsrequestlength