Я пытаюсь разработать приложение в django. У меня есть общая таблица Publication
, которая может или не может быть связана с моделью Project
.
Вот код:
Проект
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
Публикация
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
Теперь проблема в том, что я пытаюсь отобразить эту ссылку в шаблоне. Как я могу это сделать? Публикация.project.title, кажется, не работает. В общем, публикация не имеет поля проекта
Шаблон
{% for p in projects %}
<option value="">{{ p.title }}</option>
{% if p.projects.id %}
<option>{{ p.title }}</option>
{% endif %}
{% endfor %}
Чего мне не хватает?
Редактировать
@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)