ImportError:没有名为django_filters的模块

ksi*_*elp 26 django django-filter

我正在使用Django 1.7.1,我将已安装的django-filters添加到我的虚拟环境中 /.virtualenvs/auction2/lib/python2.7/site-packages$

它说已成功安装.

所以我在已安装的应用程序中放置了django-filters,如下所示:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
    'django_filters',
    'donations',
)
Run Code Online (Sandbox Code Playgroud)

我跑了python manage.py runserver,得到了这个错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Dani/.virtualenvs/auction2/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/Dani/.virtualenvs/auction2/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/Dani/.virtualenvs/auction2/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/Dani/.virtualenvs/auction2/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/Dani/.virtualenvs/auction2/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named django_filters
Run Code Online (Sandbox Code Playgroud)

它说安装了,但为什么不能导入呢?我有另一个包装,酥脆的形式,安装和工作.我在虚拟环境中查看了我的站点包,我看到:

crispy_forms
django
django_braces-1.4.0.dist-info
django_crispy_forms-1.4.0-py2.7.egg-info
django_filters-0.1.0-py2.7.egg-info
easy_install.py
easy_install.pyc
filters
pip
Run Code Online (Sandbox Code Playgroud)

看到它作为'过滤器'而不是文档所说的导入它(django_filters),我想我会尝试将它更改为只在instal_apps中的'过滤器'.

我停止并启动了runserver,没问题,所以我开始构建我的过滤器filter.py:

import django_filters
from donations.models import Donor, Item, Issue

class DonorFilter(django_filters.FilterSet):
    class Meta:
        model = Donor
        fields = {'type':['exact'],'donor':['icontains'],}

        def __init__(self, *args, **kwargs):
            super(DonorFilter, self).__init__(*args, **kwargs)
            self.filters['type'].extra.update(
                {'empty_label': 'All Types'})
Run Code Online (Sandbox Code Playgroud)

我停下来启动runserver,没问题.然后我开始在views.py上添加一个视图和import语句:

from donations.filters import DonorFilter
Run Code Online (Sandbox Code Playgroud)

给了我同样的ImportError: No module named django_filters. 错误.

我尝试将filters.py中的导入更改为过滤器而不是django_filters,并且错误没有改变.我将所有内容更改回django_filters(在installed_apps和我的filters.py中),正如文档所说,我global name 'DonorFilter' is not defined在添加视图时收到错误.这是view.py:

def donor_list(request):
    f = DonorFilter(request.GET, queryset=Donor.objects.all())
    return render_to_response('donations/donor_list', {'filter': f})
Run Code Online (Sandbox Code Playgroud)

这意味着我需要导入我在filters.py中创建的函数?所以我添加 from donations.filters import DonorFilter到我的视图顶部.那么错误就是'module' object has no attribute 'FilterSet'

我可以在virtualenv中安装的filters.py文件中看到FilterSet类

我注意到django-filter有更多的开发,https://github.com/alex/django-filter页面上升到v0.9.2,但是pip安装0.1.0.我应该以另一种方式安装它(除了点子)?

我很陌生,感谢任何帮助!

ksi*_*elp 46

我的pip版本很旧,很旧.1.5.6当我安装我的虚拟环境时,它只是工作,所以我没有质疑.学过的知识!这是我做的,以防它帮助别人......

在虚拟环境中,我按照文档中的说明安装了pip:https : //pip.pypa.io/en/stable/installing.htmlpython get-pip.py 这将我升级为pip 6.1.1

pip install django-filter
pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

阅读requirements.txt显示我有

django-filter==0.9.2
django-filters==0.1.0
Run Code Online (Sandbox Code Playgroud)

所以我用旧版本卸载了旧版本 pip uninstall django-filters

注意旧版本上的s而不是新版本上的s

真的是基本的东西,但它真的让我失望了.感谢任何花时间研究这个的人!


小智 11

我在安装 django-filter==2.2.0 时也遇到了这个问题

这是我的 Django 版本:

Django [required: >=1.11, installed: 2.2]
Run Code Online (Sandbox Code Playgroud)

设置.py:

INSTALLED_APPS = [
    # ...
    'django_filters',
]
Run Code Online (Sandbox Code Playgroud)

我错误地安装了:

pipenv install django_filters
Run Code Online (Sandbox Code Playgroud)

这是正确的安装:

pipenv install django-filter
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。


Ayu*_*yan 10

我也有同样的问题.即使安装后django-filters我也无法导入它

ImportError:没有名为django_filters的模块

我使用的Django版本是1.8,python版本是2.7.x.

解决方法是安装djangorestframework-filters

pip install djangorestframework-filters
Run Code Online (Sandbox Code Playgroud)

名字很混乱.

然而,它django-rest-framework-filters是Django REST框架和Django过滤器的扩展,可以轻松跨关系进行过滤,同时Django-filter是一个可重用的Django应用程序,允许用户以声明方式从URL参数添加动态QuerySet过滤.