title: Django display Foreign Key reference in Template
body: <p>I'm trying to develop an application in django. I have a general <code>Publication</code> table that may or may be not linked to a <code>Project</code> model. </p>
  
  <p>Here is some code :</p>
  
  <h3>Project</h3>
  
  <pre><code>class Project(models.Model):
      title = models.CharField(max_length=254)
      abstract = models.CharField(max_length=254)
      link = models.URLField()
  
      def __str__(self):
          return self.title
  </code></pre>
  
  <h3>Publication</h3>
  
  <pre><code>class Publication(models.Model):
      id = models.IntegerField(primary_key=True)
      title = models.CharField(max_length=254)
      authors_string = models.CharField(max_length=254, default='')
      project = models.ForeignKey(Project, on_delete=models.DO_NOTHING, blank=True, null=True)
  
      def __str__(self):
          return self.title
  </code></pre>
  
  <p>Now the problem is that I'm trying to display this reference in the template. How can I do that?
  publication.project.title seems not to work. In general publication seems not to have a project field</p>
  
  <h3>Template</h3>
  
  <pre><code>{% for p in projects %}
      &lt;option value=""&gt;{{ p.title }}&lt;/option&gt;
      {% if p.projects.id %}
          &lt;option&gt;{{ p.title }}&lt;/option&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>What am I missing?</p>
  
  <h3>Edit</h3>
  
  <pre><code>@login_required
  def profile(request, initial_page=1):
  
      # Get query params
      offset = (initial_page-1) * 10
      limit = offset + 10
  
      # Handle default GET request
      current_user = request.user
      pending_publications = Publication.objects.filter(authors=current_user).order_by('-year')
      number_of_pending_publications = Publication.objects.filter(authors=current_user).count()
      number_of_pending_pages = int(number_of_pending_publications/10) + 1
      confirmed_publications = Publication.objects.filter(authors2=current_user).order_by('-year')
      number_of_confirmed_publications = Publication.objects.filter(authors2=current_user).count()
      number_of_confirmed_pages = int(number_of_confirmed_publications/10) + 1
      projects = Project.objects.all()
  
      if (number_of_confirmed_publications % 10) == 0:
          number_of_confirmed_pages -=1 
      if (number_of_pending_publications % 10) == 0:
          number_of_pending_pages -=1
  
      # Set up form defaults
      default_form_values = {
          "first_name" : request.user.first_name, 
          "last_name" : request.user.last_name,
          "email" : request.user.email, 
          "organization" : request.user.organization,
          "bio" : request.user.bio,
      }
  
      # Set up context variables for html
      context = {
          'form': UpdateProfileForm(initial = default_form_values),
          'projects' : projects,
          'confirmed_publications' : confirmed_publications[offset:limit],
          'number_of_confirmed_publications' : number_of_confirmed_publications,
          'number_of_confirmed_pages' : number_of_confirmed_pages,
          'pending_publications' : pending_publications[offset:limit],
          'number_of_pending_publications' : number_of_pending_publications,
          'number_of_pending_pages' : number_of_pending_pages,
          'initial_pending_page' : initial_page,
          'initial_confirmed_page' : 1,
  
      }
  
      # Render page
      return render(request, 'main/publication.html', context)
  </code></pre>