这个if语句有什么用吗?它工作,只是不正确。没有出现错误,它会显示与每个单词值相关联的数字,但不会显示正确的单词和前端。
<p class="accent list-text">I'm a:</p>
{% if request.user.userprofile.user_type == 1 %}
<p class="profile-info list-text">Designer</p>
{% elif request.user.userprofile.user_type == 2 %}
<p class="profile-info list-text">Designer</p>
{% else %}
<p class="profile-info list-text">Both</p>
{% endif %}
Models.py:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from users.choices import *
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50,default='User')
join_date = models.DateTimeField(default=timezone.now)
profile_pic = models.ImageField(upload_to='profile_pics',null=True,blank=True)
location = models.CharField(max_length=150)
title = models.CharField(max_length=250)
user_type = models.IntegerField(choices=USER_TYPE_CHOICES,default=1)
website = models.URLField(max_length=100,blank=True)
about = models.TextField(max_length=500,default='about')
twitter = models.CharField(max_length=50,blank=True)
dribbble = models.CharField(max_length=50,blank=True)
github = models.CharField(max_length=50,blank=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()
def __str__(self):
return self.user.username
choices.py:
USER_TYPE_CHOICES = (
(1, ("Designer")),
(2, ("Developer")),
(3, ("Both")),
)
答案 0 :(得分:1)
在 UserProfile模型
中只需使用CharField
进行此类选择即可。
USER_TYPE_CHOICES = (
('1', 'Designer'),
('2', 'Developer'),
('3', 'Both'),
)
user_type = models.CharField(max_length=1,choices=USER_TYPE_CHOICES,default='1')