Django Utils six for Django 3.1.5 - django

I have a token generator that uses six
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)
)
account_activation_token = AccountActivationTokenGenerator()
This runs on Django 2.2 but my boss told me to run this on latest 3.1.5. However, six was already depreciated.
I also checked this SO suggestion https://stackoverflow.com/a/65620383/13933721
Saying to do pip install django-six then change from django.utils import six to import six
I did the installation and changes to the code but it gives error message:
ModuleNotFoundError: No module named 'six'
I think I did something wrong or something is missing but I don't know how to proceed.
Here is my pip freeze for reference
asgiref==3.3.1
Django==3.1.5
django-six==1.0.4
djangorestframework==3.12.2
Pillow==8.1.0
pytz==2020.5
sqlparse==0.4.1
EDIT
It should had been pip install six not pip install django-six
pip install six fixed my issue

Offical django page says:
Dont use it or switch to here (https://pypi.org/project/six/)

Related

ModuleNotFoundError: No module named 'django_filter'

I'm trying to use django-filter on my application but I get the error 'ModuleNotFoundError: No module named 'django_filter'
Below is my code base
settings.py
'''
...
'django_filter'
...
'''
I have also installed django_filter in my application.
Run this command
pip install django-filter
and add django_filters in you INSTALLED_APPS.
Official doc
first install django-filter using pip install django-filter
than add the django-filter in
INSTALLED_APPS=[ ... django_filter ]

ModuleNotFoundError: No module named 'stripe'

ModuleNotFoundError: No module named 'stripe' Even I Have import into my views.py fle
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.conf import settings
import stripe
STRIPE_PUBLISHABLE_KEY1=settings.STRIPE_PUBLISHABLE_KEY
# Create your views here.
def testing(request):
return render(request,"login.html",{'key':STRIPE_PUBLISHABLE_KEY1})
def charge(request): # new
if request.method == 'POST':
charge = stripe.Charge.create(
amount=500,
currency='usd',
description='A Django charge',
source=request.POST['stripeToken']
)
return render(request, 'success.html')
Always Showing Error
NameError at /charge/
name 'stripe' is not defined
May be you haven't install it in your enviroment,Try to install it using
pip install stripe
for more details see documentations https://pypi.org/project/stripe/
Did you install Stripe? if not, try:
pip install stripe
Yet another reason may be that you are using venv but that you installed the module for the wrong version of Python. For example, compare these:
# install to default Python version, which might be, say, 3.10
# ...and then you'll get module not found if you expected to install it to, say, 3.9.
pip install -r requirements.txt
# install the module for 3.9 explicitly
python3.9 -m pip install -r requirements.txt

ImportError: No module named django_filters

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!
My pip version was old, really old. 1.5.6 When I installed my virtual environment it just worked, so I didn't question. Lesson learned! Here is what I did in case it helps someone else...
In the virtual environment, I installed pip as described in the docs:
https://pip.pypa.io/en/stable/installing.html
python get-pip.py This upgraded me to pip 6.1.1
pip install django-filter
pip freeze > requirements.txt
Reading requirements.txt showed I had
django-filter==0.9.2
django-filters==0.1.0
So I uninstalled the older version with pip uninstall django-filters
notice the s on the older version but not on the new one
Really basic stuff but it really tripped me up. Thanks to anyone who took time to look into this!
I also had the same issue. Even after installing django-filters I couldn't import it
ImportError: No module named django_filters
The Django version I'm using is 1.8 and python version is 2.7.x
Solution is to install djangorestframework-filters
pip install djangorestframework-filters
The names are very confusing.
However django-rest-framework-filters is an extension to Django REST framework and Django filter that makes it easy to filter across relationships while Django-filter is a reusable Django application allowing users to declaratively add dynamic QuerySet filtering from URL parameters.
I also encountered this issue installing django-filter==2.2.0
This was my Django version:
Django [required: >=1.11, installed: 2.2]
settings.py:
INSTALLED_APPS = [
# ...
'django_filter',
]
This is the correct installation:
pipenv install django-filter
Hope it helps.
I had a similar issue using django 1.7, djangorestframework==3.2.0 and latest django-filter==0.13.0:
Using DjangoFilterBackend, but django-filter is not installed
cannot import name Expression
I finally fixed it by downgrading django-filter to 0.11.
Now pip freeze looks like this and its working:
Django==1.7
django-filter==0.11.0
djangorestframework==3.2.0
I changed from django_filter to django_filters in installed apps and it was okay.
We install it using: pip install django-filter or in case you are using a virtual environment like pipenv, use pipenv install django-filter .
the above install, adds the latest version of django-filter. Add "==version" to specify the version ( say 0.11.0) you need
In the settings.py under app, ensure it's 'django_filters',
INSTALLED_APPS = [
# ...
'django_filters',
]
I tried all of the above answers and none of them worked for me. I later learnt that the pip script is not installing modules for the interpreter that the python command is using.
SOLUTION:
python -m pip install django-filter
Ensure its django_filters and not django-filters in settings.py. Also keep in find its not django-filter, its django-filters.
But to install it:
pip install django-filter
I removed django_filters in the settings INSTALLED_APPS. this is worked for me
I also experienced the same problem but i realized its because i had installed django-filter when am in the virtual environment.
I tried installing django-filter at the same directory without activating the virtual environment and it worked
I used pip install django-filter

django on jython(cannot import name BaseDatabaseWrapper)

I have a problem buliding django on jython
I have already installed django-jython , jython , django
in settings.py dababase engine I write : doj.db.backends.sqlite
raiseImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured:
'doj.db.backends.sqlite' isn't an
available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: cannot import name BaseDatabaseWrapper
it seems that in doj.db there is no these classes which I can find in django.db
and I find in site-packages\django_jython-1.7.0b2-py2.7.egg\doj\db\backends\sqlite\base.py
there are :
from doj.db.backends import JDBCBaseDatabaseWrapper as BaseDatabaseWrapper
from doj.db.backends import JDBCBaseDatabaseFeatures as BaseDatabaseFeatures
from doj.db.backends import JDBCBaseDatabaseOperations as BaseDatabaseOperations
from doj.db.backends import JDBCCursorWrapper as CursorWrapper
from doj.db.backends import JDBCConnection
maybe the problem lies here
thanks for your help
I got the same problem. It's Django compatibility issue indeed. django-jython 1.7 works with Django 1.7.x (see https://pythonhosted.org/django-jython/release-notes.html#b2)

Integrate SASS/SCSS with Django

I want to use SASS/SCSS with Django application.
I followed the link https://bitbucket.org/synic/django-sass.
I installed SASS using sudo pip install sass.
When i run Python manage.py runserver,iam getting error as
'sass' is not a valid tag library: Template library sass not found, tried django.templatetags.sass
Can Anyone help me?!
python sass (pip install sass) https://pypi.python.org/pypi/sass is different
from django-sass (https://bitbucket.org/synic/django-sass)
Download django sass from https://bitbucket.org/synic/django-sass after that install and setup as documented .
Have you first installed sass, the ruby app?
$ apt-get install ruby-sass
You'll know if it's done properlly; as type sass on the command line, does sassy things.
Next, I cloned django-sass (from the other answer):
git clone git#bitbucket.org:synic/django-sass.git
Then navigated to the puled folder and installed it.
$ python setup.py install
Initially the installation crashed out:
IOError: [Errno 2] No such file or directory: 'CHANGES.rst'
So I quickly created the file:
touch CHANGES.rst
and ran the install command again.
no problems.
The django-sass-processor package is a great package that lets you easily integrate SASS/SCSS with Django.
Here's a tutorial on how to set up SASS/SCSS with Django.
I've used the package a few times and have been happy with it.
Here is my DIY out of the box solution
Install Libsass and Watchdog
pip install libsass watchdog
Create an app named core
python manage.py startapp core
core/sass.py
import os
import time
import site
import sass
import threading
from pathlib import Path
from django.apps import apps
from django.conf import settings
from watchdog.observers import Observer
from watchdog.events import FileClosedEvent
def compiler():
packageFolders = [
site.getusersitepackages(),
*[path for path in site.getsitepackages()],
]
staticFolders = settings.STATICFILES_DIRS.copy()
staticFolders += [
os.path.join(app.path, "static") for app in apps.get_app_configs()
]
compileFolders = staticFolders.copy()
for staticFolder in staticFolders:
for packageFolder in packageFolders:
if Path(staticFolder).is_relative_to(packageFolder):
if staticFolder in compileFolders:
compileFolders.remove(staticFolder)
if settings.DEBUG:
def watcher(path):
class Event(FileClosedEvent):
def dispatch(self, event):
filename, extension = os.path.splitext(event.src_path)
if extension == ".scss":
time.sleep(0.5)
for d in compileFolders:
if os.path.isdir(d):
try:
sass.compile(
dirname=(d, d),
output_style="expanded",
include_paths=staticFolders,
)
except sass.CompileError as error:
print(error)
event_handler = Event(path)
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
for d in compileFolders:
if os.path.isdir(d):
try:
sass.compile(
dirname=(d, d),
output_style="expanded",
include_paths=staticFolders,
)
except sass.CompileError as error:
print(error)
thread = threading.Thread(target=watcher, args=(d,), daemon=True)
thread.start()
else:
d = settings.STATIC_ROOT
if os.path.exists(d):
try:
sass.compile(
dirname=(d, d),
output_style="expanded",
include_paths=staticFolders,
)
except sass.CompileError as error:
print(error)
core/apps.py
from django.apps import AppConfig
from core.sass import compiler
class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core'
def ready(self):
compiler()
For more info, djboilerplate is a boilerplate project where I have added this out of the sass feature