[英]if-else statement in python django
I am new to Django and I have a problem that I couldn't solve.我是 Django 的新手,我有一个无法解决的问题。 I am trying to display a specific question and other related attribute from my Question model based on a field from the Participant model.
我正在尝试根据 Participant 模型中的字段显示我的 Question 模型中的特定问题和其他相关属性。 The issue here is that it directly goes to the else statement even when the condition is true.I tried to print(participant.condition) and it works so I am not sure why its not working with the if statement.
这里的问题是,即使条件为真,它也会直接转到 else 语句。我尝试打印(participant.condition)并且它有效,所以我不确定为什么它不能与 if 语句一起使用。
@login_required
def LPSC_VIEW1(request):
participant=request.user.participant
if participant.condition == 'LPN':
First_question= Question.objects.get(id=1)
all_choices = First_question.choices.all()
context = {'First_question': First_question, 'all_choices': all_choices}
return render(request, 'study/FirstQN.html', context)
else:
First_question= Question.objects.get(id=12)
all_choices = First_question.choices.all()
context = {'First_question': First_question, 'all_choices': all_choices}
return render(request, 'study/FirstQSC.html', context)
my models as the following:我的模型如下:
class Question(models.Model):
question_text = models.CharField(max_length=200)
caption = models.CharField(max_length=200, default="this is a caption")
choices = models.ManyToManyField(Choice)
vis_image = models.ImageField(default= "this is an image", null=False, blank=False, upload_to="static/study/img")
def __str__(self):
return self.question_text
class Condition(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Participant(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
condition = models.ForeignKey(Condition, on_delete=models.CASCADE)
score = models.IntegerField(default=0)
def __str__(self):
return self.user.username
You must change this:你必须改变这一点:
participant=request.user.participant
to:到:
participant=Participant.objects.get(user=request.user)
condition
is a foreign key, not a string. condition
是外键,而不是字符串。 You're comparing it against 'LPN'
, but no instance of your Condition
model will be equal to that string.您将它与
'LPN'
进行比较,但您的Condition
模型的任何实例都不会等于该字符串。
Try if participant.condition.name == 'LPN':
to compare the name field on the Condition
instance to that string.尝试
if participant.condition.name == 'LPN':
将Condition
实例上的 name 字段与该字符串进行比较。
Your print
statement shows them as apparently being the same because you've defined how to present Condition
instances as strings with your __str__
method - it will print the name for the Condition
instance, but that doesn't mean that the Condition
value is actually equal to that string.您的
print
语句显示它们显然是相同的,因为您已经定义了如何使用__str__
方法将Condition
实例呈现为字符串-它将打印Condition
实例的名称,但这并不意味着Condition
值实际上相等到那个字符串。
You might have to use你可能不得不使用
from .models import Participant
participant = Participant.objects.get(user = request.user)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.