在我的django模型中,我有15个用户,并且使用html代码将它们显示在html页面上:
{% for user in users %}
<h3>{{ user.get_full_name }}</h3>
{% endfor %}
views.py:
def user1(request):
if request.method =='POST':
if form.is_valid():
form.save()
return redirect('user1')
else:
return render(request, 'accounts/user1.html')
def user2(request):
if request.method =='POST':
if form.is_valid():
form.save()
return redirect('user2')
else:
return render(request, 'accounts/user2.html')
models.py:
class User(models.Model):
user = models.OneToOneField("auth.User", on_delete=models.CASCADE)
player_name = models.CharField(max_length=100, default='')
team = models.ForeignKey(Team, on_delete=models.CASCADE)
jersey_number = models.CharField(max_length=100, default='')
position = models.CharField(max_length=100, default='')
height = models.CharField(max_length=100, default='')
weight = models.CharField(max_length=100, default='')
grade = models.CharField(max_length=100, default='')
headshot = models.ImageField(upload_to='profile_image', blank=True)
status = models.CharField(max_length=100, choices=STATUS_CHOICES)
如何将“用户”中的每个用户与其自己的html页面联系起来。由于我没有手动写出每个用户并添加标签,因此我对如何使用此格式为每个用户提供单独的html页面感到困惑。我试图使用此代码为每个用户提供单独的个人资料页面。
views.py:
def view_profile(request, pk=None):
if pk:
user = User.objects.get(pk=pk)
else:
user = request.user
args = {'user': user}
return render(request, 'accounts/profile.html', args)