我想在我的自定义模板中实现django admin过滤器,您的任何机构可以帮助我解决此问题,我将不胜感激。我收到此错误...... 未定义名称'AddFilter' ...
models.py
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField()
description = models.TextField()
release_date = models.DateField()
filters.py
import django_filters
class ProductFilter(django_filters.FilterSet):
name = django_filters.CharFilter(lookup_expr='iexact')
class Meta:
model = Product
fields = ['price', 'release_date']
views.py
def filt_page(request):
filter = AddFilter(request.GET, queryset=Add.objects.all())
print (filter)
return render_to_response('filt_page.html',{'filter':filter})
模板
<form action="" method="get"> {% csrf_token %}
{{ filter.form.as_p }}
<input type="submit" />
</form>
{% for obj in filter %}
{{ obj.name }}<br />
{% endfor %}
答案 0 :(得分:0)
您在views中调用了一个未定义的类。py
filters.py中的类名称为'ProductFilter',但您在views.py中已将其命名为'AddFilter'
此外,过滤器中的型号名称是未定义的,您使用的是“添加”而不是“产品”
尝试
filter = ProductFilter(request.GET, queryset=Product.objects.all())