-1

models.py

class Position(models.Model):
    positions = models.CharField(max_length = 200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.positions

class Candidate(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField('User Email')
    mobile_no = models.CharField(max_length=10)
    candidate_image = models.ImageField(null=False, blank=False, upload_to="candidate_images/")
    description = models.TextField(null=True, blank=True)
    votes = models.IntegerField(default = 0)
    positionc = models.ForeignKey(Position, on_delete = models.CASCADE, related_name ='candidateobj')

    def __str__(self):
        return self.first_name

views.py

def candidate_list(request):
    candidate_list = Candidate.objects.get(pk=1)
    return render(request, 'candidate_list.html', {
        "candidate_list": candidate_list,
    })

candidate_list.html

<div class="card-header"> {{ candidate_list.position.positions }} </div>

Here I should get to see the position from the 'Position' model. But nothing is rendering for the above code. Other information are showing properly in candidate_list.html.

I checked google and found a article also this stackoverflow question. The Stackoverflow solution is the exact issue I believe and I did the similar in my code but didn't get anything in the html page. I'm new in django.

dEEPRAJ
  • 1
  • 2

1 Answers1

0

You have an error in your syntax. Your field that makes reference to position is called "positionc" instead of "position".

The solution is:

{{ candidate_list.positionc.positions }}
tdy
  • 36,675
  • 19
  • 86
  • 83