I am using Django 1.7.1 and I pip installed django-filters to my virtual env at /.virtualenvs/auction2/lib/python2.7/site-packages$
It said it was installed successfully.
So I placed django-filters in installed apps like so:
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',
)
I ran python manage.py runserver
and got this error:
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
It said it installed, but why can't it import it? I have another package, crispy-forms, installed and working. I looked at my site packages on the virtual environment and I saw:
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
Seeing that it goes in as 'filters' instead of what the documentation says to import it as (django_filters), I thought I'd try changing it to just 'filters' in installed_apps.
I stoped and started the runserver, no problem, so I began building my filter in 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'})
I stop and start the runserver, no problem. Then I start adding a view and just the import statement at views.py:
from donations.filters import DonorFilter
gives me the same ImportError: No module named django_filters.
error.
I tried changing the import in my filters.py to filters rather than django_filters and the errors didn't change. I changed everything back to django_filters (in installed_apps and my filters.py) as the documentation says to do, I get the error global name 'DonorFilter' is not defined
when I add the view. Here is the view.py:
def donor_list(request):
f = DonorFilter(request.GET, queryset=Donor.objects.all())
return render_to_response('donations/donor_list', {'filter': f})
That means I need to import the function I created in filters.py? So I add
from donations.filters import DonorFilter
to the top of my view.
Then the error is 'module' object has no attribute 'FilterSet'
I can see the FilterSet class in the filters.py file installed in my virtualenv
I noticed there is more development on django-filter, the https://github.com/alex/django-filter page goes up to v0.9.2, but pip installs 0.1.0. Should I be installing it another way (other than pip)?
I'm very new at this and appreciate any help!
See Question&Answers more detail:
os