Error: SynchronousOnlyOperation with Django 3

Ouverte
#228 8 commentaires 2 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
25/100
Type d'issue
Bug
Clarté
À clarifier
Activité
À l'abandon
Stack technique
django, python
Domaine
tooling

Piste de recherche

Start by reproducing python manage.py livereload with Django 3.1, then inspect the reported failure at formsapp/templates/forms/base.html line 18 and follow the traceback through Django's session and authentication code. Compare the behavior with the README and requirements; done means a confirmed compatibility diagnosis and a clearly documented resolution.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

state: needs info

I have Ubuntu 18.04. I've followed the instructions in the README, but when I run python manage.py livereload and open localhost:8000 I get a SynchronousOnlyOperation error. My traceback is as follows. It seems to raise error at {% user.is_authenticated %} template tag.

Environment:


Request Method: GET
Request URL: http://localhost:8000/

Django Version: 3.1
Python Version: 3.6.9
Installed Applications:
['livereload',
 'formsapp',
 'Dummy',
 'crispy_forms',
 'users',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.facebook',
 'allauth.socialaccount.providers.github',
 'allauth.socialaccount.providers.google']
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']


Template error:
In template /home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/formsapp/templates/forms/base.html, error at line 18
   You cannot call this from an async context - use a thread or sync_to_async.
   8 :     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   9 : 
   10 :     <title>{% block title %}Home page{% endblock %}</title>
   11 :     <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
   12 :     <link rel="stylesheet" href="{% static 'css/styles.css' %}">
   13 : </head>
   14 : <body>
   15 : <header>
   16 :     <div>
   17 :         <!-- User login/logout options -->
   18 :          {% if user.is_authenticated %} 
   19 :             <a href="{% url 'logout' %}">Sign out</a>
   20 :         {% else %}
   21 :             <a href="{% url 'login' %}">Sign In</a>
   22 :             <a href="{% url 'register' %}">Sign Up</a>
   23 :         {% endif %}
   24 :         User: {{ user }}
   25 :     </div>
   26 :     <h1>
   27 :         <a id="logo" href="{% url 'home-page' %}">Website name & logo</a>
   28 :     </h1>


Traceback (most recent call last):
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 215, in _get_session
    return self._session_cache

During handling of the above exception ('SessionStore' object has no attribute '_session_cache'), another exception occurred:
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/formsapp/views.py", line 10, in show_homepage
    return render(request, 'forms/base.html')
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/defaulttags.py", line 305, in render
    match = condition.eval(context)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/defaulttags.py", line 889, in eval
    return self.value.resolve(context, ignore_failures=True)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/base.py", line 671, in resolve
    obj = self.var.resolve(context)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/base.py", line 796, in resolve
    value = self._resolve_lookup(context)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/template/base.py", line 829, in _resolve_lookup
    current = current[bit]
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/utils/functional.py", line 240, in inner
    self._setup()
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/utils/functional.py", line 376, in _setup
    self._wrapped = self._setupfunc()
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/auth/middleware.py", line 23, in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/auth/middleware.py", line 11, in get_user
    request._cached_user = auth.get_user(request)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 174, in get_user
    user_id = _get_user_session_key(request)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 58, in _get_user_session_key
    return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 65, in __getitem__
    return self._session[key]
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 220, in _get_session
    self._session_cache = self.load()
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 43, in load
    s = self._get_session_from_db()
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 34, in _get_session_from_db
    expire_date__gt=timezone.now()
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/db/models/query.py", line 425, in get
    num = len(clone)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/db/models/query.py", line 269, in __len__
    self._fetch_all()
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1303, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1152, in execute_sql
    cursor = self.connection.cursor()
  File "/home/raphaelnazirullah/Documents/Mental Health Web app/mental_health_app/venv/lib/python3.6/site-packages/django/utils/asyncio.py", line 24, in inner
    raise SynchronousOnlyOperation(message)

Exception Type: SynchronousOnlyOperation at /
Exception Value: You cannot call this from an async context - use a thread or sync_to_async.

My project structure is as follows:

.
├── db.sqlite3
├── Dummy
├── formsapp
├── manage.py
├── mental_health_app
├── my-cred.txt
├── __pycache__
├── README.md
├── requirements.txt
├── users
├── venv
Langage dominant
Python
Étoiles
1.1k
Forks
148
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de lepture/python-livereload

Toutes les issues de lepture/python-livereload

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.