简体   繁体   中英

if-else statement in python django

I am new to Django and I have a problem that I couldn't solve. I am trying to display a specific question and other related attribute from my Question model based on a field from the Participant model. 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.

@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. You're comparing it against 'LPN' , but no instance of your Condition model will be equal to that string.

Try if participant.condition.name == 'LPN': to compare the name field on the Condition instance to that string.

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.

You might have to use

from .models import Participant
participant = Participant.objects.get(user = request.user)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM