如何在模板中呈现用户名?
How can I render username in the templates?
我创建了一个用户可以注册和登录的应用程序和 post 一个视频,如何在 post 中呈现用户名。我想在每个 post 用户中传递一个用户名。喜欢社交网络
这是我的模型
from django.db.models.fields import CharField
from django.contrib.auth.models import User
from django.db import models
Create your models here.
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
file = models.FileField(null=False, blank=False)
title = CharField(max_length=25, blank=False)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
这是我的模板首页模板:
<div class="container">
{% for post in posting %}
<div class="row justify-content-center">
<div class="col">
<div class="card">
<video
id="my-video"
class="video-js"
controls
width="640"
height="264"
poster="MY_VIDEO_POSTER.jpg"
data-setup="{}"
type="video/mp4"
>
<source src="{{post.file.url}}">
</video>
<div class="card-body">
<small>{{post.date_added}}</small>
<br>
<small>{{post.title}}</small>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
这是我的观点:
from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
from .models import Post
def dashboard(request):
posting = Post.objects.all()
return render(request, 'dashboard.html', {'posting': posting})
这是我的登录视图:
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
return redirect('home')
else:
messages.info(request, 'Invalid Credential')
return redirect('login')
else:
return render(request, 'login.html')
这是我的注册意见:
def register(request):
if request.method == 'POST':
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email Already taking')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'username is taken')
return redirect('register')
else:
user = User.objects.create_user(username=username, email=email, password=password)
user.save();
return redirect('login')
else:
messages.info(request, 'Password Not Match')
return redirect('register')
return redirect ('/')
else:
return render(request, 'signup.html')
我想在 post
中传递一个用户名
您可以使用 post.user.username
访问用户的用户名,例如:
<div class="card-body">
<small>{{post.date_added}} <strong>by {{ post.user.username }}</strong></small>
<br>
<small>{{post.title}}</small>
</div>
在视图中,您可以通过在单个查询中使用 Post
选择相关用户来提高性能:
def dashboard(request):
posting = Post.objects<strong>.select_related('user')</strong>
return render(request, 'dashboard.html', {'posting': posting})
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
我创建了一个用户可以注册和登录的应用程序和 post 一个视频,如何在 post 中呈现用户名。我想在每个 post 用户中传递一个用户名。喜欢社交网络
这是我的模型
from django.db.models.fields import CharField
from django.contrib.auth.models import User
from django.db import models
Create your models here.
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
file = models.FileField(null=False, blank=False)
title = CharField(max_length=25, blank=False)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
这是我的模板首页模板:
<div class="container">
{% for post in posting %}
<div class="row justify-content-center">
<div class="col">
<div class="card">
<video
id="my-video"
class="video-js"
controls
width="640"
height="264"
poster="MY_VIDEO_POSTER.jpg"
data-setup="{}"
type="video/mp4"
>
<source src="{{post.file.url}}">
</video>
<div class="card-body">
<small>{{post.date_added}}</small>
<br>
<small>{{post.title}}</small>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
这是我的观点:
from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
from .models import Post
def dashboard(request):
posting = Post.objects.all()
return render(request, 'dashboard.html', {'posting': posting})
这是我的登录视图:
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
return redirect('home')
else:
messages.info(request, 'Invalid Credential')
return redirect('login')
else:
return render(request, 'login.html')
这是我的注册意见:
def register(request):
if request.method == 'POST':
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
password2 = request.POST['password2']
if password == password2:
if User.objects.filter(email=email).exists():
messages.info(request, 'Email Already taking')
return redirect('register')
elif User.objects.filter(username=username).exists():
messages.info(request, 'username is taken')
return redirect('register')
else:
user = User.objects.create_user(username=username, email=email, password=password)
user.save();
return redirect('login')
else:
messages.info(request, 'Password Not Match')
return redirect('register')
return redirect ('/')
else:
return render(request, 'signup.html')
我想在 post
中传递一个用户名您可以使用 post.user.username
访问用户的用户名,例如:
<div class="card-body">
<small>{{post.date_added}} <strong>by {{ post.user.username }}</strong></small>
<br>
<small>{{post.title}}</small>
</div>
在视图中,您可以通过在单个查询中使用 Post
选择相关用户来提高性能:
def dashboard(request):
posting = Post.objects<strong>.select_related('user')</strong>
return render(request, 'dashboard.html', {'posting': posting})
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.