在django中获取登录用户的用户名

时间:2018-04-28 14:59:20

标签: python django login username

我是Django的新手,我只想找到一种方法来获取登录用户的用户名并使用它来获取用户的主板。我可能通过添加另一个用户模型来完成模型错误,但我想获得用户用户名的一些帮助。这是我的观点文件:

    from django.shortcuts import render
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate,login
from django.contrib.auth.models import User
from .models import board_member,user
# Create your views here.

def index(request):
# Login form submitted?
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        if username and password:
            user = authenticate(username=username, password=password)
            # Login succeeded
            if user is not None:
                login(request, user)
                return HttpResponseRedirect(reverse('login_success'))
        # Login failed
        return HttpResponseRedirect(reverse('login_fail'))
    return render(request, 'Boards/login.html')

def login_success(request):
    return render(request, 'Boards/Homepage.html')

def getboards(request):
    username = request.user.get_username()
    boards = []
    for Memberships in board_member.objects.all():
        if username == Memberships.user.username:
            boards.append(Memberships.board)
    return render(request, 'Boards/Homepage.html', {'boards': boards, 'username':username})

def login_fail(request):
    return render(request, 'login_fail.html')

这是我的模型文件:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class board(models.Model):
    name = models.CharField(max_length=20)

    def __str__(self):
        return (self.name)

class user(models.Model):
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=20)
    boards = models.ManyToManyField(
        board, through='board_member', related_name="members")

    def __str__(self):
        return(self.username)


class list(models.Model):
    title = models.CharField(max_length=20)
    board = models.ForeignKey(
        board, related_name="lists", on_delete=models.CASCADE)

class board_member(models.Model):
    user = models.ForeignKey(
        user, related_name="memberships", on_delete=models.CASCADE)
    board = models.ForeignKey(
        board, related_name="memberships", on_delete=models.CASCADE)
    access = models.CharField(max_length=20)

    def __str__(self):
        return ("%s is %s in board %s" % (self.user, self.access, self.board))


class card(models.Model):
    list = models.ForeignKey(list, related_name="cards",
                             on_delete=models.CASCADE)
    text = models.CharField(max_length=100)
    date = models.DateField(auto_now=True)
    due = models.DateField()
    flag = models.CharField(max_length=20)

2 个答案:

答案 0 :(得分:1)

通常要获取登录用户的用户名

username = request.user.username. 在视图内部。

但是我不确定你为什么要创建另一个用户模型。我想你正在尝试扩展现有的auth用户模型。

答案 1 :(得分:1)

首先,您需要实施django custom user model并添加额外字段boards 在视图中,你可以得到所有的董事会,如

request.user.boards.all()