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'^$',
employees.employee_list,
name='contacts_employee_list'),
url(r'^(?P<employee_id>\d+)/$',
employees.employee_detail,
name='contacts_employee_detail'),
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!
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.