[PYTHON] How to uniquely identify the source of access in the Django Generic Class View

2020-04-08 Created: windows10 / Python-3.8.2-amd64 / Django-3.0.4

Logging the source IP address isn't enough for Django to analyze user behavior. To uniquely identify a user, it's easy to log who accessed it on the HTTP server.

Customize the response headers to keep the Django user ID in the HTTP server logs. Here's how to add the Django user ID to the request headers in the generic class view.

reference https://blog.howtelevision.co.jp/entry/2014/09/05/170917

If you're new to Django, click here. Practical tutorial on Django in 10 minutes

Define a view class with a response header added

Add the response header userid you want to add to the generic view class.

custom_views.py


class CustomListView(generic.ListView):
    def dispatch(self, *args, **kwargs):
        response = super().dispatch(*args, **kwargs)
        response['userid'] = self.request.user
        return response

Define the view class to actually call

Instead of inheriting ListView, inherit CustomListView and declare the actual view class to use.

views.py


from .custom_views import *
from .models import MyClass
from django.contrib.auth.mixins import LoginRequiredMixin

class MemoListView(LoginRequiredMixin, CustomListView):
    model = MyClass

The same can be done with other view classes as well as ListView.

If you change the settings of an HTTP server such as Nginx and leave the newly created response header in the log, the access source will be recorded in the log.

The end

Recommended Posts

How to uniquely identify the source of access in the Django Generic Class View
How to use bootstrap in Django generic class view
How to upload files in Django generic class view
How to check the version of Django
Ajax in Django (using generic class view)
How to count the number of elements in Django and output to a template
How to get the number of digits in Python
How to access the global variable of the imported module
How to identify the element with the smallest number of characters in a Python list?
How to write custom validations in the Django REST Framework
How to find the optimal number of clusters in k-means
How to use the __call__ method in a Python class
How to set the html class attribute in Django's forms.py
How to generate a query using the IN operator in Django
In Django, how to abbreviate the long displayed string as ....
How to create a record by pasting a relation to the inheriting source Model in the Model inherited by Django
How to reflect CSS in Django
The meaning of ".object" in Django
How to handle multiple versions of CUDA in the same environment
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
How to change the color of just the button pressed in Tkinter
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
[TensorFlow 2] How to check the contents of Tensor in graph mode
How to get the vertex coordinates of a feature in ArcPy
View the implementation source code in iPython
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to delete expired sessions in Django
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
I want to explain the abstract class (ABCmeta) of Python in detail.
How to get a namespaced view name from a URL (path_info) in Django
How to use __slots__ in Python class
How to access the Datastore from the outside
How to do Server-Sent Events in Django
How to implement Scroll View in pythonista 1
How to convert DateTimeField format in Django
How to deal with garbled characters in json of Django REST Framework
How to view images in Django's Admin
How to pass the execution result of a shell command in a list in Python
How to mention a user group in slack notification, how to check the id of the user group
How to access the contents of a Linux disk on a Mac (but read-only)
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to find the coefficient of the trendline that passes through the vertices in Python
I'm addicted to the difference in how Flask and Django receive JSON data
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
How to make the font width of jupyter notebook put in pyenv equal width
How to get a list of files in the same directory with python
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
How to calculate the volatility of a brand
How to use the C library in Python
How to keep track of work in Powershell
How to implement Rails helper-like functionality in Django
Summary of how to import files in Python 3
View the result of geometry processing in Python
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
Summary of how to use MNIST in Python
How to print debug messages to the Django console
How to implement "named_scope" of RubyOnRails with Django