【发布时间】:2022-01-17 10:58:41
【问题描述】:
我正在创建一个页面,允许用户过滤数据集、点击搜索并查看下面的结果更新。每当我将 week 函数更新为 pick_list.html 而不是 filter_list.html 时,我都会收到以下错误。我的 filter_list.html 没有显示来自我的应用程序其余部分的任何引导视觉效果或颜色,只是一个简单的列表。 pick_list.html 显示一个搜索按钮,但没有搜索字段和零数据。
为什么我如此费力地将这两者结合起来?
views.py:
def week(request):
#orders = Pick.objects.get(id=pk_test)
orders = Pick.objects.all()
#orders = week.order_set.all()
myFilter = PickFilter(request.GET, queryset=orders)
orders = myFilter.qs
context = {'week':week, 'orders':orders, 'myFilter':myFilter}
return render(request, 'app/filter_list.html',context)
models.py
class Pick(models.Model):
submitter = models.CharField(max_length=50, verbose_name='Submitter', null=True, blank=True)
week = models.CharField(max_length=50, verbose_name='Week', null=True, blank=True)
name = models.CharField(max_length=50, verbose_name='Name', null=True, blank=True)
photo = models.CharField(max_length=50, verbose_name='Photo', null=True, blank=True)
#photo = models.ImageField(upload_to="media", max_length=500, verbose_name='Photo', null=True, blank=True)
hometown = models.CharField(max_length=50, verbose_name='Hometown', null=True, blank=True)
age = models.IntegerField(verbose_name='Age', null=True, blank=True)
progress = models.IntegerField(verbose_name='Progress', null=True, blank=True)
occupation = models.CharField(max_length=50, verbose_name='Occupation', null=True, blank=True)
elim_week = models.CharField(max_length=50, verbose_name='Week Eliminated', null=True, blank=True)
rank = OrderField(verbose_name='Rank', null=True, blank=True)
def __str__(self):
return self.name
def get_fields(self):
return [(field.verbose_name, field.value_from_object(self)) for field in self.__class__._meta.fields]
def get_absolute_url(self):
return reverse('app:pick-update', kwargs={'pk': self.pk})
class Meta:
pick_list.html:
{% extends 'base.html' %}
{% load cms_tags %}
{% block title %} {{ title }} · {{ block.super }} {% endblock title %}
{% block content %}
<div style="font-size:24px">
{{ title }}
</div>
<div style="font-size:14px; margin-bottom:15px">
Click on the arrows on the right of each contestant and drag them up or down to reorder them based on how far you think they are going to go.
</div>
<form method="get">
{{ myFilter.form }}
<button type="submit">Search</button>
</form>
<ul>
<table class="table table-hover" id="table-ajax" style="background-color: white;">
<thead style="background-color: #de5246; color:white; border-bottom:white">
<tr>
{% comment %} <th></th> {% endcomment %}
<th style="width: 50px; text-align: center;"></th>
<th>{{ object_list|verbose_name:'field:name' }}</th>
<th>{{ object_list|verbose_name:'field:hometown' }}</th>
<th>{{ object_list|verbose_name:'field:occupation' }}</th>
<th>{{ object_list|verbose_name:'field:age' }}</th>
<th>Progress</th>
<th style="width: 160px; text-align: center;">Rank</th>
</tr>
</thead>
<tbody class="order" data-url="{% url 'cms:reorder' model_name %}">
{% include 'app/filter_list.html' %}
</tbody>
</table>
{% endblock %}
filter_list.html:
{% load static %}
{% load cms_tags %}
{% for order in orders %}
<tr id="{{ order.id }}">
<td><img src="http://127.0.0.1:8000/media/files/{{ order.photo }}" width="50"/></td>
<td><a href="" title="Leads" style="text-decoration: none">{{ order.name }}</a></td>
<td>{{ order.hometown }}</td>
<td>{{ order.occupation }}</td>
<td>{{ order.age }}</td>
<td>
<div class="progress">
<div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="{{ order.age }}" aria-valuemin="0" aria-valuemax="100" style="width: 75%"></div>
</div>
</td>
<td style="text-align: center;">
<a href="" class="btn btn-sm border-0 reorder" title="Reorder">
<i class="fa fa-sort text-secondary"></i></a>
</td>
</tr>
{% empty %}
<tr class="table-warning nosort">
<td colspan="100%" class="text-center"><small class="text-muted">No {{ model_verbose_name_plural|lower }}</small>
</td>
</tr>
{% endfor %}
<tr class="table-light table-sm nosort">
<td colspan="100%"><small class="text-muted">Total rows: {{ orders.count }}</small></td>
</tr>
错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/picks/filter/
Django Version: 3.2.6
Python Version: 3.8.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'account',
'cms',
'app',
'widget_tweaks',
'django_cleanup',
'bootstrap_datepicker_plus']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template C:\Users\New User\Downloads\django-modal-ajax-crud-main\django-modal-ajax-crud-main\templates\base.html, error at line 0
'str' object has no attribute '_meta'
视图的回溯错误:
context
{'myFilter': <app.filters.PickFilter object at 0x04CC9FB8>,
'orders': <QuerySet [<Pick: Claire>, <Pick: Elizabeth>, <Pick: Cassidy>, <Pick: Eliza>, <Pick: Daria>, <Pick: Ency>, <Pick: Gabby>, <Pick: Genevieve>, <Pick: Hailey>, <Pick: Hunter>, <Pick: Hunter>]>}
myFilter
<app.filters.PickFilter object at 0x04CC9FB8>
orders
<QuerySet [<Pick: Claire>, <Pick: Elizabeth>, <Pick: Cassidy>, <Pick: Eliza>, <Pick: Daria>, <Pick: Ency>, <Pick: Gabby>, <Pick: Genevieve>, <Pick: Hailey>, <Pick: Hunter>, <Pick: Hunter>]>
request
<WSGIRequest: GET '/picks/filter/'>
【问题讨论】: