I am new to Django, and I just took over from another developer on this project. All I have done so far is clone the code from git and install the dependencies.
Immediately after setting up the project, and running python manager.py runserver and going to localhost:8000/admin I get an error stating the TypeError at /admin/login/, 'CsrfViewMiddleware' object is not iterable:
Traceback:
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/core/handlers/exception.py"
in inner
39. response = get_response(request)
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/core/handlers/base.py"
in _legacy_get_response
249. response = self._get_response(request)
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/core/handlers/base.py"
in _get_response
217. response = self.process_exception_by_middleware(e, request)
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/core/handlers/base.py"
in _get_response
215. response = response.render()
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/template/response.py"
in render
109. self.content = self.rendered_content
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/template/response.py"
in rendered_content
86. content = template.render(context, self._request)
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/template/backends/django.py"
in render
66. return self.template.render(context)
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/template/base.py"
in render
206. with context.bind_template(self):
File "/usr/lib/python3.5/contextlib.py" in __enter__
59. return next(self.gen)
File
"/home/abhay/code/virtualenvironments/leaguesx/lib/python3.5/site-packages/django/template/context.py"
in bind_template
236. updates.update(processor(self.request))
Exception Type: TypeError at /admin/login/
Exception Value: 'CsrfViewMiddleware' object is not iterable
I would post code from the source code but I can't figure where in the source the cause of this might possibly be.
My settings.py:
import os
from datetime import datetime
from django.conf.global_settings import EMAIL_USE_SSL
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'ourapp',
'social.apps.django_app.default',
'sendgrid',
'corsheaders',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'ourapp.middleWare.authenticationMiddleware.AuthenticationMiddleware'
)
ROOT_URLCONF = ''
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.middleware.csrf.CsrfViewMiddleware',
'corsheaders.middleware.CorsMiddleware',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
],
},
},
]
(Sorry about the lack of indentation.)
Any ideas on how to proceed from here would be greatly appreciated!
Try removing 'django.middleware.csrf.CsrfViewMiddleware', from TEMPLATES. Probably 'corsheaders.middleware.CorsMiddleware', too
Related
I am getting an error on the login page when I log out.
ValueError at /accounts/login/
dictionary update sequence element #0 has length 0; 2 is required
I do not know why I get this error because I've never had such an error before. What does this error mean and how can I solve it?
traceback
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/login/?next=/
Django Version: 3.1.4
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'register',
'customer',
'financial_analysis',
'ocr',
'core',
'approvals',
'crispy_forms',
'ckeditor',
'rest_framework',
'requests',
'ckeditor_uploader',
'django_filters',
'activity_log',
'djmoney',
'djmoney.contrib.exchange',
'mathfilters',
'bootstrap3',
'phone_field']
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']
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\core\handlers\base.py", line 202, in _get_response
response = response.render()
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\response.py", line 105, in render
self.content = self.rendered_content
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\response.py", line 83, in rendered_content
return template.render(context, self._request)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 168, in render
with context.bind_template(self):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\context.py", line 244, in bind_template
updates.update(processor(self.request))
Exception Type: ValueError at /accounts/login/
Exception Value: dictionary update sequence element #0 has length 0; 2 is required
register/urls.py
from django.conf.urls import url
from django.conf.urls.static import static
from django.urls import path, include
from CreditReviewBot import settings
from . import views
from .views import update_user
urlpatterns = [
url(r'^signup/$', views.signup, name='signup'),
path('', views.home, name='home'),
path('accounts/', include('django.contrib.auth.urls')),
url(r'^accounts/(?P<id>\d+)/update/$', update_user, name="update"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'approvals.views.approval_context_processor',
],
},
},
]
I am trying to install pip3 install django-allauth to prevent login before email confirmation, but at moment when setting up settings.py I get the following error. I don't want to rename all the project names, because it is going to affect the complete project. Is there a way to achieve this without too many changes?
xxxxxx#xxxxxx:~/Documents/blackowl$ python3 manage.py makemigrations account
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/xxxxxx/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_com
mand_line
utility.execute()
File "/home/xxxxxx/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/xxxxxx/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/xxxxxx/.local/lib/python3.6/site-packages/django/apps/registry.py", line 95, in populate
"duplicates: %s" % app_config.label)
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: account
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'allauth' ,
'allauth.account' ,
'allauth.socialaccount' ,
'allauth.socialaccount.providers.github' ,
'apps.website',
'apps.account'
]
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',
]
ROOT_URLCONF = 'blackowl.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
Try to add this under INSTALLED_APPS :
'django.contrib.account'
I am using WEasyPrint and Jinja2 to generate a PDF out of a template, and have the following code:
env = Environment(loader=FileSystemLoader(self.app.templates_path))
template = env.get_template("order_scheduler/pdf_order_description.html") # ERROR
html_out = template.render()
The problem is that I am getting AttributeError: 'FileSystemLoader' object has no attribute 'get_source' on the second line of the code. Tried to surf the internet, but didn't find any sources on this.
I think, settings file content might be helpful. Find it below:
INSTALLED_APPS = [
#...
'django_jinja',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'supermarket_project.urls'
#ANONYMOUS_USER_ID=-1
TEMPLATES = [
{
"BACKEND": "django_jinja.backend.Jinja2",
"APP_DIRS": True,
"OPTIONS": {
"match_extension": ".jinja",
}
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(BASE_DIR),"static","templates")],
#'DIRS': [os.path.join(BASE_DIR,"templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages'
],
},
},
]
WSGI_APPLICATION = 'supermarket_project.wsgi.application'
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = '/static/'
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR),"static","static"),
)
The full traceback:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 148, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 146, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 184, in inner
return func(*args, **kwargs)
File "C:\Users\edgar\Desktop\djprj\djprj\vendor\views.py", line 383, in get_pdf
template = env.get_template("pdf_order_description.html")
File "C:\Python27\lib\site-packages\jinja2\environment.py", line 812, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Python27\lib\site-packages\jinja2\environment.py", line 774, in _load_template
cache_key = self.loader.get_source(self, name)[1]
AttributeError: 'FileSystemLoader' object has no attribute 'get_source'
The content of pdf_order_description.html is as simple as <p>Stackoverflow</p>
Any ideas how to fix the error ?
Not sure why you're doing that stuff with env and FileSystemLoader at all.
Django already has support for Jinja2, and you've configured that in your settings. You should use the standard template loading functionality to get your Jinja template:
html_out = render_to_string('order_scheduler/pdf_order_description.jinja')
Note that you've configured the template loader to expect the extension "jinja" to load Jinja templates, so you should use that for your template.
I don't know which FileSystemLoader you are using. maybe the wrong import in your code. django_jinja wraps the loader.
https://github.com/niwinz/django-jinja/blob/master/django_jinja/loaders.py
vs. how a jinja Template looks like.
https://github.com/pallets/jinja/blob/master/jinja2/loaders.py
Maybe you just need to specify the TEMPLATE_LOADERS
https://github.com/niwinz/django-jinja/blob/b8073c2ae8f7c8350ab695c637b6a55e04998f69/testing/settings.py#L71
I was trying internationalization/localization in django.
I am getting an error while i am trying to make the '.po' files using the command
./manage.py makemessages
relevant parts from settings.py
import os
from django.utils.translation import ugettext_lazy as _
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'sampleproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n'
],
},
},
]
WSGI_APPLICATION = 'sampleproject.wsgi.application'
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGES = [
('fi-FI', _('Finnish')),
('en', _('English')),
]
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
relevant parts from urls.py
urlpatterns += i18n_patterns(
url(r'^$', home, name='home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
here is the traceback.
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/base.py", line 305, in run_from_argv
self.execute(*args, **cmd_options)
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/base.py", line 356, in execute
output = self.handle(*args, **options)
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 361, in handle
potfiles = self.build_potfiles()
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 393, in build_potfiles
self.process_files(file_list)
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 488, in process_files
self.process_locale_dir(locale_dir, files)
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 507, in process_locale_dir
build_file.preprocess()
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 113, in preprocess
content = templatize(src_data, self.path[2:])
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 214, in templatize
return _trans.templatize(src, origin)
File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 670, in templatize
"%s (%sline %d)" % (t.contents, filemsg, t.lineno)
SyntaxError: Translation blocks must not include other block tags: blocktrans count var|length as count (file htmlcov/_project_Myapps_for_sample_lib_python3_4_site-packages_django_templatetags_i18n_py.html, line 1073)
Dev Setup:
Django 1.10
Python 3.4.5
As this is my first question in SO, pardon me if there's any mistake :)
Thanks in advance :)
The error occurred because of the htmlcov folder which was generated while running the coverage script.
Removed that folder and executed the following commands to generate the '.po' files.
./manage.py makemessages -l fi
and the following command to generate the '.mo' files.
./manage.py compilemessages
Django Attribute Error: 'function' object has no attribute 'is_usable'
I am setting up a Django blog. I followed these instructions and I am getting the following error:
Environment:
Request Method: GET
Request URL: http://url.com/
Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'divinare',
'rest_framework',
'django_facebook',
'django.contrib.auth')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
139. response = response.render()
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
80. template = self.resolve_template(self.template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/response.py" in resolve_template
56. return loader.select_template(template)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in select_template
180. return get_template(template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
138. template, origin = find_template(template_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
121. loader = find_template_loader(loader_name)
File "/home/spencer/webapps/hello_django/local/lib/python2.7/site-packages/django/template/loader.py" in find_template_loader
104. if not func.is_usable:
Exception Type: AttributeError at /
Exception Value: 'function' object has no attribute 'is_usable'
Middleware:
MIDDLEWARE_CLASSES = (
'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 Loaders:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.contrib.auth.context_processors.auth',
# 'django.template.loaders.eggs.Loader',
)
Any insights to what is causing the error would be very helpful.
Your problem is almost certainly that you have "django.contrib.auth.context_processors.auth" in your TEMPLATE_LOADERS setting. As the name implies, that's a context processor, not a template loader - it should go in TEMPLATE_CONTEXT_PROCESSORS.