使用列表、详细信息通用视图填充模板
我正在创建数据库中存在的用户列表,它们通过使用通用视图显示在 user_list.html 模板中,但我的模型从模型中的其他类继承了它的许多属性。现在我希望当用户单击他的名字时,他应该被重定向到 user_detail.html 页面,并且他应该在这里获取他的详细信息。 详细信息将从数据库中获取,它只是从定义查询集的同一模型中选取值。
我的views.py看起来像
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, get_object_or_404
from django.views.generic.list_detail import object_list, object_detail
from contacts.models import *
def employee_list(request, queryset=None, **kwargs):
if queryset is None:
queryset = Employee.objects.all()
return object_list(
request,
queryset=queryset,
paginate_by=20,
**kwargs)
def employee_detail(request, employee_id):
return object_detail(
request,
queryset= Employee.objects.all(),
# extra_context ={"EC_list": EmergencyContact.objects.all()},
object_id=employee_id)
urls.py
from contacts.views import employees
urlpatterns = patterns('',
url(r'^$',
employees.employee_list,
name='contacts_employee_list'),
url(r'^(?P<employee_id>\d+)/$',
employees.employee_detail,
name='contacts_employee_detail'),
我的employee_deatil.html看起来像
{% block title %} Employee details {% endblock %}
{% block heading1%}<h1> Employee's Details </h1>{% endblock %}
{% block right_menu %}
{% if object %}
<ul>
<li> Name:{{ object.full_name }}</li>
<li> Contact No.: {{ object.phone_number }}</li>
<!-- <li> Refrence Contact No.: {{ EC_list.contact }}</li> -->
<li> Blood Group: {{ object.blood_type }}</li>
<li> Martial Status: {{ object.martial_status }}</li>
<li> Nationality: {{ object.about }}</li>
<!-- <li> Relationship: {{ EC_list.relationship }}</li>
<li>Course: {{ object.course }}</li> -->
</ul>
{% else %}
No Registered user present.
{% endif %}
{% endblock %}
所以请帮助我弄清楚如何显示其他模型中存在的员工的所有数据。谢谢你!
I am creating a list of users present in my database, they are being displayed in user_list.html template through the use of generic views, but my models inherit many of its properties from other classes in the model. Now I want that when a user clicks on his name he should be redirected to the user_detail.html page and he should get his details here.
The details are to be picked up from the database, it is just picking the values from the same model for which the queryset is defined.
my views.py looks like
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, get_object_or_404
from django.views.generic.list_detail import object_list, object_detail
from contacts.models import *
def employee_list(request, queryset=None, **kwargs):
if queryset is None:
queryset = Employee.objects.all()
return object_list(
request,
queryset=queryset,
paginate_by=20,
**kwargs)
def employee_detail(request, employee_id):
return object_detail(
request,
queryset= Employee.objects.all(),
# extra_context ={"EC_list": EmergencyContact.objects.all()},
object_id=employee_id)
urls.py
from contacts.views import employees
urlpatterns = patterns('',
url(r'^
my employee_deatil.html looks like
{% block title %} Employee details {% endblock %}
{% block heading1%}<h1> Employee's Details </h1>{% endblock %}
{% block right_menu %}
{% if object %}
<ul>
<li> Name:{{ object.full_name }}</li>
<li> Contact No.: {{ object.phone_number }}</li>
<!-- <li> Refrence Contact No.: {{ EC_list.contact }}</li> -->
<li> Blood Group: {{ object.blood_type }}</li>
<li> Martial Status: {{ object.martial_status }}</li>
<li> Nationality: {{ object.about }}</li>
<!-- <li> Relationship: {{ EC_list.relationship }}</li>
<li>Course: {{ object.course }}</li> -->
</ul>
{% else %}
No Registered user present.
{% endif %}
{% endblock %}
So please help me to figure out that how can I display all the data of employee which is present in the other models. Thank you!
,
employees.employee_list,
name='contacts_employee_list'),
url(r'^(?P<employee_id>\d+)/
my employee_deatil.html looks like
So please help me to figure out that how can I display all the data of employee which is present in the other models. Thank you!
,
employees.employee_detail,
name='contacts_employee_detail'),
my employee_deatil.html looks like
So please help me to figure out that how can I display all the data of employee which is present in the other models. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您想显示存储在其他模型中的员工的信息。
我假设您知道可以在视图中进行预过滤并使用查询集发送额外的上下文变量。使用现有行
extra_context ="EC_list": EmergencyContact.objects.all()
选择太多。我假设您的 EmergencyContact 模型有 Employee 的外键(
employee=ForeignKey(Employee, related_name='emergency_contacts')
)。在这种情况下,您必须过滤额外的上下文。这将过滤列表以仅显示您需要的紧急联系人。
当然,这只是一种方法。如果您不需要对紧急联系人进行任何奇特的过滤器,则可以在模板中使用外键反向查找。即去掉 EC_list 的 extra_context 并将联系人渲染函数替换为:
请记住,我们有
employee =foreignKey(Employee, related_name='emergency_contacts')
作为从 EmergencyContact 到 Employee 的外键。此声明不仅将员工字段添加到 EmergencyContact,而且还向 Employee 添加了一个名为“emergency_contacts”的额外虚拟字段。该虚拟字段返回链接到当前员工的所有紧急联系人的查询集。如果您有任何问题或需要文档链接,请告诉我
编辑:为了可读性,请考虑设置通用视图的 template_object_name 参数。
If i understand you correctly, you want to display information about the employee which is stored in other models.
I assume you know that you can pre-filter in the view and send an extra context variable with a queryset. using your existing line
extra_context ="EC_list": EmergencyContact.objects.all()
selects too many. I assume your EmergencyContact model has a foreign key to Employee(
employee = ForeignKey(Employee, related_name='emergency_contacts')
). In this case you must pass your extra context filtered.this will filter the list to just the emergency contacts you need.
Of course, this is only one way to do it. if you don't need any fancy filters on emergency contacts, you can use foreign key reverse lookups within the template. i.e. get rid of the extra_context for EC_list and replace the contact rendering function with this:
Remember that we have
employee = ForeignKey(Employee, related_name='emergency_contacts')
as a foreign key from EmergencyContact to Employee. not only does this declaration add the employee field to EmergencyContact but it adds an extra virtual field to Employee with the name 'emergency_contacts'. this virtual field returns a queryset of all Emergency Contacts linked to the current employee.Let me know if you have any questions or need links to documentation
EDIT: for readability sake, considder setting the template_object_name parameter of the generic view.