Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3423016
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:18:25+00:00 2026-05-18T06:18:25+00:00

I am creating a list of users present in my database, they are being

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:18:26+00:00Added an answer on May 18, 2026 at 6:18 am

    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.

    def employee_detail(request, employee_id):
        return object_detail(
            request,
            queryset= Employee.objects.all(),
            extra_context ={"EC_list": EmergencyContact.objects.filter(employee__pk=employee_id)},
            object_id=employee_id)
    

    this will filter the list to just the emergency contacts you need.

    {% 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> Blood Group: {{ object.blood_type }}</li>
            <li> Martial Status: {{ object.martial_status }}</li>
            <li> Nationality: {{ object.about }}</li>
            <li> Course: {{ object.course }}</li>
            <li> Emergency Contacts: 
              <ul> 
                {% for EC in EC_list %} 
                <li> Name: {{ EC.name }} </li>
                <li> Contact No.: {{ EC.contact }}</li>
                <li> Relationship: {{ EC.relationship }}</li>
                {% endfor %}
              </ul>
            </li>
         </ul>
         {% else %}
             No Registered user present.
         {% endif %}
     {% endblock %}
    

    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:

            <li> Emergency Contacts: 
              <ul> 
                {% for EC in object.emergency_contacts %} 
                <li> Name: {{ EC.name }} </li>
                <li> Contact No.: {{ EC.contact }}</li>
                <li> Relationship: {{ EC.relationship }}</li>
                {% endfor %}
              </ul>
            </li>
    

    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.

    def employee_detail(request, employee_id):
        return object_detail(request, queryset= Employee.objects.all(),
                      object_id=employee_id, template_object_name='employee')
    
    ------------------------------------------------------------------------------------------
    
            <li> Name: {{ employee.full_name }}</li>
            <li> Emergency Contacts: 
              <ul> 
                {% for EC in employee.emergency_contacts %} 
                <li> Name: {{ EC.name }} </li>
                <li> Contact No.: {{ EC.contact }}</li>
                <li> Relationship: {{ EC.relationship }}</li>
                {% endfor %}
              </ul>
            </li>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a Java application where the user can search through a list of
OK, let's say I am creating a program that will list users contacts in
I was wondering how I could go about creating a list of users four
I'm creating a simple ajax call to retrieve a list of user reviews. I
I'm creating a list of class Task in a way such as this. List<Task>
Why does this attempt at creating a list of curried functions not work? def
What is the best construction for creating a List of Strings? Is it Lists.newArrayList()
I have an ARM project that I'm building with make. I'm creating the list
I am creating a calendar list system, that has tabbed dates on top, with
I am creating a search list in java. If I enter the beginning letters,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.