django
How can I log both successful and failed login and logout attempts in Django?
You could hook up to the provided signals: django.contrib.auth.signals Recording to log import logging from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed from django.dispatch import receiver log = logging.getLogger(__name__) @receiver(user_logged_in) def user_logged_in_callback(sender, request, user, **kwargs): # to cover more complex cases: # http://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django ip = request.META.get(‘REMOTE_ADDR’) log.debug(‘login user: {user} via ip: {ip}’.format( user=user, ip=ip )) @receiver(user_logged_out) def … Read more
How to use visual studio code to debug django
For VSCode (full disclosure, I’m one of the VSCode developers) try installing the Python extension to get started. This documentation covers debugging Django. There should be a included debug configuration or you can add your own to the launch.json file: { “name”: “Django”, “type”: “python”, “request”: “launch”, “stopOnEntry”: false, “pythonPath”: “${config.python.pythonPath}”, “program”: “${workspaceRoot}/manage.py”, “args”: [ … Read more
Select a COUNT using Django model?
Use count(): >>> Model.objects.count() 42 >>> Model.related_set.count() 102 >>> Model.related_set.filter(blah=42).count() 3
InterfaceError: connection already closed (using django + celery + Scrapy)
Unfortunately this is a problem with django + psycopg2 + celery combo. It’s an old and unsolved problem. Take a look on this thread to understand: https://github.com/celery/django-celery/issues/121 Basically, when celery starts a worker, it forks a database connection from django.db framework. If this connection drops for some reason, it doesn’t create a new one. Celery … Read more
Django: Best way to merge migrations conflicts
From the Django docs: Because migrations are stored in version control, you’ll occasionally come across situations where you and another developer have both committed a migration to the same app at the same time, resulting in two migrations with the same number. Don’t worry – the numbers are just there for developers’ reference, Django just … Read more
What is @csrf_exempt in Django?
Normally when you make a request via a form you want the form being submitted to your view to originate from your website and not come from some other domain. To ensure that this happens, you can put a csrf token in your form for your view to recognize. If you add @csrf_exempt to the … Read more
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment varia
Use this import os os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘mysite.settings’ instead of os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “<appname>.settings”)
Python 3.7, Failed building wheel for MySql-Python
You need to install the following dependencies before installing mysqlclient for python 3.7 in your system. sudo apt-get install python3.7-dev default-libmysqlclient-dev I hope this will help you.