how to get user details from request method in django
To get user details from a request method in Django, you can use the request.user attribute. This attribute returns the user object associated with the current request, or an anonymous user object if the user is not authenticated.
Here's an example of how to get the user's username and email address from a request method:
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
@login_required
def my_view(request):
username = request.user.username
email = request.user.email
return HttpResponse(f"Hello {username}, your email is {email}")
In this example, the login_required decorator is used to ensure that only authenticated users can access the view. The request.user attribute is then used to retrieve the user's username and email address, which are then included in the HTTP response.
原文地址: https://www.cveoy.top/t/topic/8Vm 著作权归作者所有。请勿转载和采集!