Check User online Django 1.10 - django

Hi trying to do like in example:
How to check whether a user is online in django template?
but when adding
'userprofile.middleware.ActiveUserMiddleware', to MIDDLEWARE_CLASSES
recieving next error:
Unhandled exception in thread started by <function wrapper at 0x7fcf6e0b8668>
Traceback (most recent call last):
File "/home/pavlo/.local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/pavlo/.local/lib/python2.7/site-packages/channels/management/commands/runserver.py", line 40, in inner_run
http_consumer=self.get_consumer(*args, **options),
File "/home/pavlo/.local/lib/python2.7/site-packages/channels/management/commands/runserver.py", line 136, in get_consumer
return StaticFilesConsumer()
File "/home/pavlo/.local/lib/python2.7/site-packages/channels/handler.py", line 332, in __init__
self.handler = self.handler_class()
File "/home/pavlo/.local/lib/python2.7/site-packages/channels/staticfiles.py", line 18, in __init__
super(StaticFilesHandler, self).__init__()
File "/home/pavlo/.local/lib/python2.7/site-packages/channels/handler.py", line 179, in __init__
self.load_middleware()
File "/home/pavlo/.local/lib/python2.7/site-packages/django/core/handlers/base.py", line 82, in load_middleware
mw_instance = middleware(handler)
TypeError: this constructor takes no arguments
Thx for help.
May be someone advise how to make check for online/offline users in Django 1.10

Middlewares have changed in Django 1.10, see this guide how to upgrade them

Related

Trying to run a basic django code. When I run 'python manage.py runserver', I get this:

Just created a project, added app definition in settings.py, and created an app with a urls.py file seperately and tried to include the urls.py file in the main urls.py file that is created by default when we create a project and tried to return a HttpResponse in a seprate file.
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 591, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
self.run()
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 409, in check
messages.extend(check_resolver(pattern))
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Sujit\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'hello.urls' from 'C:\\Users\\Sujit\\Documents\\CS50\\lecture3\\hello\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I believe the error message is pretty clear:
The included URLconf <module 'hello.urls' from 'C:\Users\Sujit\Documents\CS50\lecture3\hello\urls.py'> does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
Make sure this file contains a variable named urlpatterns, and that you can succesfully import it.

ImportError: No module named encrypted_fields - Do I need to install django-encrypted-fields in settings.py? If so, how?

I followed the instructions on https://github.com/defrex/django-encrypted-fields but importing still doesn't work.
I tried adding encrypted_fields/django_encrypted_fields/django-encrypted-fields to INSTALLED_APPS in settings.py but none of those work.
How can I fix this?
Thanks!
Thanks to #Alasdair for the tip:
I installed django-encrypted-fields in the wrong virtualenv. Uninstalled it, installed it in the correct one and it imports.
However, now I'm having the same issue with keyczar, which I re-installed with django-encrypted-fields into the right virtualenv. Here is the full traceback:
Unhandled exception in thread started by <function wrapper at 0x7fee03e112a8>
Traceback (most recent call last):
File "/home/hsvar/.virtualenvs/djangodev/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/hsvar/.virtualenvs/djangodev/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/hsvar/.virtualenvs/djangodev/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/hsvar/.virtualenvs/djangodev/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/hsvar/.virtualenvs/djangodev/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/hsvar/.virtualenvs/djangodev/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/hsvar/.virtualenvs/djangodev/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named keyczar

django-seo setting up, models aren't loaded yet

I'm trying to add django-seo into my site. But I can't cope with setting up. I followed instructions documentation, but error occurs.
This is what I did:
Installed django-seo packacge
Added rollyourown.seo to INSTALED_APPS
Created seo.py file in my site content app
And this is what I wrote into seo.py file:
from rollyourown import seo
class Metadata(seo.Metadata):
title = seo.Tag(head=True, max_length=68)
description = seo.MetaTag(max_length=155)
keywords = seo.KeywordTag()
heading = seo.Tag(name="h1")
class Meta:
seo_views = ('SiteContent',)
seo_models = ('SiteContent',)
When Meta class is removed, I can't add any meta tags to contnet via Django Admin Site( I registered it in admin site ). I've read that django-seo use get_absolute_url() to deal with it. But in my site app I don't use this function for provide more some utilities to multilanguage.
But if i add Meta class, i will get this error:
Traceback (most recent call last):
File "F:/Site/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "F:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "F:\Python27\lib\site-packages\django\core\management\__init__.py", line 354, in execute
django.setup()
File "F:\Python27\lib\site-packages\django\__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "F:\Python27\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "F:\Python27\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "F:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\models.py", line 10, in <module>
__import__(module_name)
File "F:\Site\SiteContent\seo.py", line 5, in <module>
class Metadata(seo.Metadata):
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\base.py", line 166, in __new__
options = Options(Meta, help_text)
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\options.py", line 19, in __init__
self._set_seo_models(meta.pop('seo_models', []))
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\options.py", line 96, in _set_seo_models
seo_models.extend(models.get_models(app))
File "F:\Python27\lib\site-packages\django\db\models\__init__.py", line 54, in alias
return getattr(loading, function_name)(*args, **kwargs)
File "F:\Python27\lib\site-packages\django\utils\lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "F:\Python27\lib\site-packages\django\apps\registry.py", line 168, in get_models
self.check_models_ready()
File "F:\Python27\lib\site-packages\django\apps\registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
I tried diffrent solutions but nothing helped.
Django-SEO is not compatible with Django 1.7, which is the first version of Django that includes the AppRegistry.
Either rollback to Django 1.6.x or remove Django-SEO.
I got it working by changing get_query_set() to get_queryset()(changed in django1.8) in file rollyourown\seo\backends.py
As original Django-SEO is not supported anymore, these guys made and support their own version / fork of Django-SEO. I use it with Django 1.8.8, Python 3 is supported too.
https://github.com/whyflyru/django-seo

Django 1.7 upgrade error: AppRegistryNotReady using serializers from rest_framework

I get this traceback:
Traceback (most recent call last):
File "./manage.py", line 38, in <module>
execute_from_command_line(sys.argv)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/mgregory/Documents/cm_central/cmh_server/models.py", line 88, in <module>
class VersionSerializer(serializers.ModelSerializer):
File "/Users/mgregory/Documents/cm_central/cmh_server/models.py", line 89, in VersionSerializer
brzs= BrzSerializer(many=True)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 198, in __init__
self.fields = self.get_fields()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 234, in get_fields
default_fields = self.get_default_fields()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 732, in get_default_fields
reverse_rels = opts.get_all_related_objects()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 498, in get_all_related_objects
include_proxy_eq=include_proxy_eq)]
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 510, in get_all_related_objects_with_model
self._fill_related_objects_cache()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 533, in _fill_related_objects_cache
for klass in self.apps.get_models(include_auto_created=True):
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
self.check_models_ready()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
from this code (models.py):
# Serializers for transmitting CMx install information over HTTP
class BrzSerializer(serializers.ModelSerializer):
class Meta:
model = Brz
fields=('filename',)
class VersionSerializer(serializers.ModelSerializer):
brzs= BrzSerializer(many=True)
class Meta:
model = Version
fields=('name', 'for_mac', 'for_windows', 'brzs')
It sounds like it's telling me "your VersionSerializer can't have a BrzSerializer, because you haven't registered that yet".
I've looked at other SO questions relating to AppRegisteryNotReady, but didn't find one that matches this symptom. Surely I have to be able to define a chain of dependent models like this?
It turns out that having the declaration of the serializers inside models.py is causing that application to be used before the app registry has finished being loaded.
models.py is actually the wrong place to declare these serializers (though I'm 99% certain I did it that way based on an example of how to use them).
The fix is to move the declaration of the serializers out into their own file (which makes sense, because they have nothing to do with the database schema, which models.py is defining), and import that from the view. By the time the view gets going, the app registry is ready.

django, gravatar - add_to_builtins('gravatar.gravatar') causes crash on page load

add_to_builtins('gravatar.gravatar')
is being used in my settings.py file, and it causes a crash on page load with this error:
Traceback (most recent call last):
File "C:\development\python\Lib\site-packages\django\core\servers\basehttp.py", line 283, in run
self.result = application(self.environ, self.start_response)
File "C:\development\python\Lib\site-packages\django\contrib\staticfiles\handlers.py", line 68, in call
return self.application(environ, start_response)
File "C:\development\python\Lib\site-packages\django\core\handlers\wsgi.py", line 272, in call
response = self.get_response(request)
File "C:\development\python\Lib\site-packages\django\core\handlers\base.py", line 169, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "C:\development\python\Lib\site-packages\django\core\handlers\base.py", line 203, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "C:\development\python\Lib\site-packages\django\views\debug.py", line 59, in technical_500_response
html = reporter.get_traceback_html()
File "C:\development\python\Lib\site-packages\django\views\debug.py", line 128, in get_traceback_html
t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 template')
File "C:\development\python\Lib\site-packages\django\template\base.py", line 108, in init
self.nodelist = compile_string(template_string, origin)
File "C:\development\python\Lib\site-packages\django\template\base.py", line 135, in compile_string
parser = parser_class(lexer.tokenize())
File "C:\development\python\Lib\site-packages\django\template\debug.py", line 34, in init
super(DebugParser, self).init(lexer)
File "C:\development\python\Lib\site-packages\django\template\base.py", line 208, in init
print lib.tags
AttributeError: 'NoneType' object has no attribute 'tags'
If i don't have any issue with using gravatar if i don't use the add_to_builtins functionality - that is, with plain old load tags in my views. I'm just using it a fair bit, so thought i might make use of the add_to_builtins functionality.
From the looks of things, the imported version crashes because the import_library(module) for gravatar.gravatar returns None (i know this because i printed out that line, and it returns None) - what is wrong with what i've done?
oh gravatar.
Ok, so i was all wrong in how to import this using the add_to_builtins - what i needed to do was:
add_to_builtins('gravatar.templatetags.gravatar')
i'm unsure as to why, but that works a treat. Now i don't need to have any messy "load" statements, and i can happily call
{% gravatar story.user 40 %}
from any of my templates. yay!