python - 如何在 Django 模板上显示对象列表?
问题描述
我找到的所有答案都不适用于我的问题。
我正在尝试显示用户在模板“registro_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})
网址.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 %}
¿ 我错过了什么?帮助。
解决方案
您需要在您的视图中传递data
给。render
return render(request,'denuncias/registro_denuncias.html', data)
推荐阅读
- javascript - 如果函数参数可能是两种不同的类型,如何解决 TypeScript 错误?
- linux - 如何验证 USB 串行选项驱动程序是否适用于 arm-linux 板?
- java - Spark 中的 ClassCastException 读取 Teradata 和写入 Parquet
- python - 块大小是什么意思
- c++ - C++ CppUnitTest 更改 DLL 布局
- django - Django-tables2 :使用 linkify 指向记录本身
- visual-studio-code - Julia 的 vsCode 中的退出代码 3221225477。我该如何解决?
- matlab - 无法使用 DAGNetwork 类型的值作为 Matlab 中的索引
- string - System.FormatException:字符串未被识别为 IOS xamarin 表单中的有效日期时间
- ffmpeg - ffmpeg filtergraph描述中的文件索引3无效