Categories
Python Answers

How to see the raw SQL queries Python Django is running?

To see the raw SQL queries Python Django is running, we print the connection.queries value.

For instance, we add

from django.db import connection
print(connection.queries)

in our app to print the SQL queries being run.

Categories
Python Answers

How to get user IP address in Python Django?

To get user IP address in Python Django, we get the HTTP_X_FORWARDED_FOR request header.

For instance, we write

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

to create the get_client_ip function that takes the view’s request object.

And we get the user IP address with

request.META.get('HTTP_X_FORWARDED_FOR')

If that’s not available, we use

request.META.get('REMOTE_ADDR')

to get the user’s IP address.

Categories
Python Answers

How to execute code when Python Django starts once only?

To execute code when Python Django starts once only, we can put our code in the AppConfig class.

For instance, we write

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = 'myapp'
    verbose_name = "My Application"
    def ready(self):
        pass # startup code here

to create the MyAppConfig that inherits from the AppConfig class.

And we put our startup code in the ready method.

Then in myapp/__init__.py, we add

default_app_config = 'myapp.apps.MyAppConfig'

to set the default_app_config to the path to our MyAppConfig class in our app to make Django run it on startup.

Categories
Python Answers

How to manage local vs production settings in Python Django?

To manage local vs production settings in Python Django, we can create separate settings files for each environment.

For instance, in settings/local.py, we add the dev environment settings like:

from project.settings.base import *

DEBUG = True
INSTALLED_APPS += (
    'debug_toolbar', # and other apps for local development
)

And we create a settings/production.py file with the production environment settings like

from project.settings.base import *

DEBUG = False
INSTALLED_APPS += (
    # other apps for production site
)

Then we run our app with the settings file we want by running

./manage.py runserver 0:8000 --settings=project.settings.local

to run our app with the settings/local.py file.

And we run

 ./manage.py shell --settings=project.settings.production

to run our app with the settings/production.py file.

Categories
Python Answers

How to filter query objects by date range in Python Django?

To filter query objects by date range in Python Django, we can use the filter method.

For instance, we write

Sample.objects.filter(date__range=["2021-01-01", "2021-01-31"])

to call objects.filter with the date__range parameter set to a list with the date range with the strings as the date values.

date is the column name we’re filtering by.