简体   繁体   中英

Django: how to get an object of manager on admin page

这是图像 I am new to Django and I am working on a project. In my project an admin will have the power to assign the project to a manager. So I want to render the name of all the managers from the database so that it will be displayed to the admin.

here is my .html file where I want to render the name of the manager in:

<div class="body table-responsive">

    <table class="table table-hover">

      <thead>
        <tr>
            <th>S No.</th>
             <th>COMPANY NAME</th>
             <th>TEAM MEMBER</th>
              <th>EMAIL</th>
              <th>ASSIGN TEAM</th>
           </tr>
            </thead>
            <tbody>

                {%for team in object%}
                   <tr>
                      <form id="form_id" method="POST" action = "{% url 'accept' %}">
                         {% csrf_token %}

                           <th scope="row"> {{ forloop.counter }}</th>
                            <td>{{team.company_name}}</td>
                            <td>{{team.team_member}}</td>
                            <td>{{team.email}}</td>
                             <td>
                             <select name="manager">
                            {% for manager in managers %}

                             <option value ="{{manager.id}}">{{manager.name}}</option>

                              {% endfor %}                                   
                                            </select>
                                        <!-- </div> -->
                                    </td>
                                </tr>

                                {% endfor %}

                            </tbody>
                        </table>

Here is my model for manager:

class manager(models.Model):
name = models.CharField(max_length= 500)
designation = models.CharField(max_length= 500)
class Meta:
    permissions = [
        ("edit_task", "can edit the task"),
    ]

def __str__(self):
    return self.name

Here is my views.py ;

def accept(request):
obj= Create_Team.objects.filter(status='Accept') 
if request.method == 'POST':
   acc = manager()
   manager_id = int(request.POST.get('manager', 1))
   acc.manager = manager.objects.get(pk=manager_id)

return render(request, "admin/accept.html", {"object": obj})

In the admins page, I want to display all the names of the managers. I have added the image of the admin page.

I think you forgot to include the managers queryset in the context variable

def accept(request):
    obj= Create_Team.objects.filter(status='Accept')
    managers = manager.objects.all()
    if request.method == 'POST':
       acc = manager()
       manager_id = int(request.POST.get('manager', 1))
       acc.manager = manager.objects.get(pk=manager_id)

    return render(request, "admin/accept.html", {"object": obj, "managers": managers})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM