Я новичок в 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)