London-35
London-35

Reputation: 85

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

Upvotes: 0

Views: 364

Answers (3)

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

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.

Upvotes: 2

Vikrant Chauhan
Vikrant Chauhan

Reputation: 43

You might have to use

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

Upvotes: 0

shoytov
shoytov

Reputation: 595

You must change this:

participant=request.user.participant

to:

participant=Participant.objects.get(user=request.user)

Upvotes: 2

Related Questions