我正在显示用户列表。现在,我想要一个搜索下拉列表,该列表将显示公司,并根据这些公司显示用户列表。请为我提供一种同时搜索和列出用户的方法。我正在与你们分享图像。提前致谢。
class UserListView(LoginRequiredMixin, generic.TemplateView):
template_name = 'users/users.html'
def get_context_data(self, **kwargs):
context = super(UserListView, self).get_context_data(**kwargs)
context['users'] = User.objects.exclude(userprofile__user_role__role_title='Super Admin')
# Pass Form to display inside search box
return context
Users.html
{% extends "base.html" %}
{% block content %}
<div class="col-lg-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-sm-4">
<h2 class="card-title">List of Users</h2>
</div>
<div class="col-sm-5">
<form action="">
<div class="input-group">
<input type="text" class="form-control" placeholder="Type " aria-label="Recipient's username" aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-primary mr-2" type="submit">Search</button>
</div>
</div>
</form>
</div>
<div class="col-sm-3">
<a class="btn btn-info mr-2 float-right" href="{% url 'register' %}" role="button">Add a User</a>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>
#
</th>
<th>
User
</th>
<th>
First Name
</th>
<th>
Company
</th>
<th>
Role
</th>
<th>Status</th>
<th>Created</th>
<th>
Edit
</th>
<th>
Delete
</th>
</tr>
</thead>
<tbody>
{% for users in users %}
<tr>
<td>
{{ forloop.counter }}
</td>
<td class="py-1">
{% if users.userprofile.user_image %}
<img src="{{ users.userprofile.user_image.url }}" alt="image"/>
{% endif %}
</td>
<td>
{{ users.first_name }}
</td>
<td>
{{ users.userprofile.user_company }}
</td>
<td>
{{ users.userprofile.user_role }}
</td>
<td>{{ users.userprofile.user_status }}</td>
<td>{{ users.userprofile.user_created |date:"M d, Y" }}</td>
<td>
<a href="{{ users.id }}/profile-update"><i class="fa fa-edit fa-2x"></i></a>
</td>
<td>
<a href="{{ users.id }}/profile-delete"><i class="fa fa-trash-o fa-2x"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock content %}
Models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_company = models.ForeignKey(Company, on_delete=models.CASCADE)
user_role = models.ForeignKey(Roles, on_delete=models.CASCADE)
user_dob = models.DateField(null=True, blank=True)
user_phone = models.CharField(max_length=30, blank=True, null=True)
user_image = models.ImageField(upload_to='user_profiles', default='default_user.png',blank=True, null=True)
user_created = models.DateTimeField(auto_now_add=True)
class Company(models.Model):
company_name = models.CharField(max_length=20, unique=True)
company_description = models.CharField(max_length=100)
company_address = models.CharField(max_length=100)