“Dapatkan Django Pengguna” Kode Jawaban

Impor Pengguna di Django

>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', '[email protected]', 'johnpassword')

# At this point, user is a User object that has already been saved
# to the database. You can continue to change its attributes
# if you want to change other fields.
>>> user.last_name = 'Lennon'
>>> user.save()
Clever Crocodile

LoginRequiredMixin

from django.contrib.auth.mixins import LoginRequiredMixin

LOGIN_URL = 'your_url'
Virgin Programmer

Dapatkan Django Pengguna

def sample_view(request):
    current_user = request.user
    print current_user.id
Awful Alpaca

Periksa Auth Pengguna Django

from django.conf import settings
from django.shortcuts import redirect

def my_view(request):
    if not request.user.is_authenticated:
        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
    # ...
Puzzled Puffin

Cara Mendapatkan ID Pengguna Django

if request.user.is_authenticated:
    # Do something for authenticated users.
else:
    # Do something for anonymous users.
Awful Alpaca

Jawaban yang mirip dengan “Dapatkan Django Pengguna”

Pertanyaan yang mirip dengan “Dapatkan Django Pengguna”

Lebih banyak jawaban terkait untuk “Dapatkan Django Pengguna” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya