Home>
Customized login authentication with Django.
When I tried to get the login user after logging in, it became Anonymoususer,
Unable to get login user.
I would like to know the cause.
self.request.user becomes Anonymoususer
Applicable source code
class AccountAuthBackend(ModelBackend):
def authenticate(self, request, screen_id=None, password=None, **kwargs):
try:
user = User.objects.get(screen_id=screen_id)
except User.DoesNotExist:
return None
if user.check_password(password) and self.user_can_authenticate(user):
return user
else:
return None
What I tried
Add the following line to settings.py
SESSION_ENGINE ='django.conrtib.sessions.backends.cache'
Adding the above line causes the following error
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/lib64/python3.6/site-packages/django/core/servers/basehttp.py", line 45, in get_internal_wsgi_application
return import_string(app_path)
File "/usr/local/lib64/python3.6/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:],package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/var/www/html/wto_web_app/wto_web_app/wsgi.py", line 18, in<module> application = get_wsgi_application()
File "/usr/local/lib64/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/wsgi.py", line 127, in __init__
self.load_middleware()
File "/usr/local/lib64/python3.6/site-packages/django/core/handlers/base.py", line 37, in load_middleware
mw_instance = middleware(handler)
File "/usr/local/lib64/python3.6/site-packages/django/contrib/sessions/middleware.py", line 15, in __init__
engine = import_module(settings.SESSION_ENGINE)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:],
package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_loadFile "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named'django.conrtib'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib64/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib64/python3.6/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
handler = self.get_handler(*args, **options)
File "/usr/local/lib64/python3.6/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/usr/local/lib64/python3.6/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
return get_internal_wsgi_application()
File "/usr/local/lib64/python3.6/site-packages/django/core/servers/basehttp.py", line 50, in get_internal_wsgi_application
) from err
django.core.exceptions.ImproperlyConfigured: WSGI application'wto_web_app.wsgi.application' could not be loaded;Error importing module.
wsgi is defined as follows
WSGI_APPLICATION ='hogehoge_app.wsgi.application'
Python3.6.8
Django3.0.8
CentOS8.2.2004
_
-
Answer # 1
Related articles
- python - about image deletion in django
- python - i want to change the ip address that the django app gets by default from remote_addr to http_x_forwarded_for in middlew
- python - vscode: i can't debug django apps
- python 3x - please teach about the exchange of information with qthead
- python - about x-axis adjustment in matplotlib
- python - about "" "of" "" select === = "" "
- python, about the fire spread step of forest fire simulation
- python - about hamiltonian neural networks
- python - about write loop to csv
- about python argument and data definition
- about error handling during implementation of user registration function in django
- python 3x - about functions used in python for statement
- python 3x - about downloading anaconda
- python - about the optimum angle of rotation matrix
- python - django i want to filter the data associated with a foreign key and display it in a template
- python - about downloading youtube videos by youtube-dl
- about processing to exclude the character string group specified from list in python
- python - what i don't understand about yolo9000 (v2)
- about batch change of file name using python
- about the python speedtest code
Related questions
- Python and Java. Combination
- python : Do not save fields in the form of Django
- python : Redirect /on the page without a layer Django
- python : It is necessary that the "Delete" Django button appears next to each comment.
- python : Models.cascade need help! Cleared one table, and with it the other cleared. Django 3.x.
- python : Not transmitted input data in Django Form
- javascript : Django does not see CSS files, IMG, JS in Static
- python : Gives an error Page not Found in Django
- python : ATTRIBUTEERROR: 'AnonymousUser' Object Has No Attribute '_meta' User Model Supplement
- python : Errors when starting Django project
The description of template was incorrect.
I got it with the following description.
{% if user.is_authenticated %}
{{ user.get_username }}
{% else %}