M
M
Meekey2018-04-25 22:49:04
Django
Meekey, 2018-04-25 22:49:04

How to display a list of users in a template?

It is not possible to display all users who are owner in the template. It turns out only the first, one.
models.py

class Application(models.Model):

  STATUS_CHOICES = (
    ('In the work', 'В работе'),
    ('New', 'Новая'),
    ('Complited', 'Завершена')
  )

  author = models.ForeignKey(User, related_name = 'author', null = True, on_delete=models.CASCADE)
  title = models.CharField(max_length=50)
  text = models.TextField()
  room = models.CharField(max_length = 5)
  published_date = models.DateField(blank=True, null=True, default = timezone.now)
  status = models.CharField(max_length=15, choices=STATUS_CHOICES, default='New')
    
  def __str__(self):
    return self.title

class Executor(models.Model):
  application = models.ForeignKey('Application', on_delete=models.CASCADE)
  owner = models.ForeignKey(User, related_name = 'owner', null = True, blank = True, 
      on_delete=models.CASCADE, limit_choices_to={ 'groups__name': 'Техническая поддержка'})

admin.py
class ExecutorInline(admin.TabularInline):
  model = Executor
  insert_after = 'status'
  extra = 1
class ApplicationAdmin(admin.ModelAdmin):
  inlines = [ExecutorInline]

admin.site.register(Application, ApplicationAdmin)

views.py
def application_detail(request, pk):
    post = get_object_or_404(Application, pk=pk)
    own = get_object_or_404(Executor, pk=request.user.id)
    return render(request, 'helpdesk/application_detail.html', {'post': post, 'own': own})

html
<li>Исполнитель(и): {{own.owner.get_full_name}}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2018-04-25
@Meekey

def application_detail(request, pk):
    application = get_object_or_404(Application, pk=pk)
    return render(request, 'helpdesk/application_detail.html', {'application': application})

html
<li>Исполнитель(и): {% for executor in application.executor_set.all %}{{ executor.owner.get_full_name }}{% if not forloop.last %}, {%endif %}{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question