125👍
You can use the request object to find the logged in user
def my_view(request):
username = None
if request.user.is_authenticated():
username = request.user.username
According to https://docs.djangoproject.com/en/2.0/releases/1.10/
In version Django 2.0 the syntax has changed to
request.user.is_authenticated
49👍
request.user.get_username()
or request.user.username
, former is preferred.
Since the User model can be swapped
out, you should use this method instead of referencing the username
attribute directly.
P.S. For templates, use {{ user.get_username }}
- [Django]-Django 2 – How to register a user using email confirmation and CBVs?
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
- [Django]-Annotate a queryset with the average date difference? (django)
13👍
‘request.user‘ has the logged in user.
‘request.user.username‘ will return username of logged in user.
- [Django]-In Django – Model Inheritance – Does it allow you to override a parent model's attribute?
- [Django]-Make the first letter uppercase inside a django template
- [Django]-How do I return JSON without using a template in Django?
- [Django]-Django: Multiple forms possible when using FormView?
- [Django]-Numeric for loop in Django templates
- [Django]-Django QuerySet order
6👍
You can use this to get the logged-in user’s username :-
Just write this in template.
{{ request.user.username }}
- [Django]-How do I use Django templates without the rest of Django?
- [Django]-IntegrityError duplicate key value violates unique constraint – django/postgres
- [Django]-Embedding JSON objects in script tags
3👍
if you are using the old way of writting views, in the way of Function-Based-Views…
in your view, you are creating a new variable called usuario
to save the request.user probably…
but if you returning to the Template
a context_instance
, passing the value of the Context of the request, you will get the logged user, just by accessing the request
.
// In your views file
from django.shortcuts import render_to_response
from django.template import RequestContext
def your_view(request):
data = {
'formulario': Formulario()
# ...
}
return render_to_response('your_template.html',
data, context_instance=RequestContext(request))
// In your template
<form id='formulario' method='POST' action=''>
<h2>Publica tu tuit, {{ request.user.username.title }} </h2>
{% csrf_token %}
{{ formulario.as_p }}
<p><input type='submit' value='Confirmar' /></p>
</form>
- [Django]-Django admin ManyToMany inline "has no ForeignKey to" error
- [Django]-Django 1.7 – makemigrations not detecting changes
- [Django]-Github issues api 401, why? (django)
2👍
request.user.get_username()
will return a string of the users email.
request.user.username
will return a method.
- [Django]-In Django, how does one filter a QuerySet with dynamic field lookups?
- [Django]-Form with CheckboxSelectMultiple doesn't validate
- [Django]-Passing STATIC_URL to file javascript with django
2👍
in this way you can check is user is logged in or not If yes for example go to profile if not back to login page
def profile(request):
try:
if request.user.username in User:
return render(request, "profile.html", {'username': request.user.username})
except:
return redirect("/accounts/login")
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-Find Monday's date with Python
- [Django]-Stack trace from manage.py runserver not appearing
0👍
You can use HttpRequest.user and get_user(request) to get the username of the logged-in user in Django Views as shown below:
# "views.py"
from django.shortcuts import render
from django.contrib.auth import get_user # Here
def test(request):
print(request.user.username) # John
print(get_user(request).username) # John
return render(request, 'index.html')
And, you can get the username of the logged-in user in Django Templates as shown below:
{% "index.html" %}
{{ user }} {% John %}
{{ user.username }} {% John %}
{{ user.get_username }} {% John %}
- [Django]-How do I perform query filtering in django templates
- [Django]-Django: Safely Remove Old Migrations?
- [Django]-Django: accessing session variables from within a template?
-1👍
For template, you can use
{% firstof request.user.get_full_name request.user.username %}
firstof will return the first one if not null else the second one
- [Django]-ImportError: No module named 'django.core.urlresolvers'
- [Django]-Django FileField upload is not working for me
- [Django]-Django: manage.py does not print stack trace for errors