Pass these specific model objects to one template

20 views
Skip to first unread message

Pemby

unread,
Mar 22, 2016, 2:48:46 AM3/22/16
to Django users
Person Model

class Person( models.Model ):
    first_name
= models.CharField( max_length=50 )
    last_name
= models.CharField( max_length=50 )    

   
def __str__(self):
       
return "{0} {1}".format( self.first_name, self.last_name )    

View Function
def getPersonData(request):
    currPersonData
= {}    

   
# get current user
    currentUser
= request.user
   
# create a person object based on who is logged in.
    person
= Person.objects.create(first_name=currentUser.first_name,
                                   last_name
=currentUser.last_name)  
                                   
# getting front loaded personMeta, that user CANNOT provide    

   
# get front-loaded person data based on who is currently logged in
    personDetails
= PersonMeta.objects.filter(legal_first_name=currentUser.first_name,
                                              legal_last_name
=currentUser.last_name).values()
   
# setting hash key for dict
    currUserKey
= "{0} {1}".format(currentUser.first_name, currentUser.last_name)

   
# setting dictionary
   
# data[currUserKey] = currentUser
   
# if person details is not false
   
if (personDetails):
        currPersonData
[currUserKey] = personDetails    

   
return currPersonData    

View Call
def signup(request):
    currPersonData
= getPersonData( request )
   
return render( request, '/signup.html/', {'data': sorted( currPersonData.items( ) )}, )

URL

    url(r'^signup/$', views.signup),    

Template

<!DOCTYPE html>
<html lang="en">
<head>
   
<meta charset="UTF-8">
   
<title>SignUp</title>
</head>
<body>    

    {% for tuple in data %}
   
<p>Tuple key: {{ tuple.0 }}</p>
    {% for key, value in tuple.1.0.items %}
       
<p>Dict key, value: {{ key }}: {{ value }}</p>
    {% endfor %}
{% endfor %}    


    {% for values in eb %}
       
<p>values</p>
    {% endfor %}    

</body>
</html>    


All of the above seems to be working the way I expect it to. I can get the data I am after and pass it to a template. Now I would like take all objects, of the below model, and pass all these objects it to my '/signup.html/' so I can start from processing.

class Elective( models.Model ):
    elective_title
= models.CharField( max_length=100 )
    elective_description
= models.CharField( max_length=100 )    

   
def __str__(self):
       
return "{0}".format( self.elective_title )    


I tried following Django Pass Multiple Models to one Template

and Refer to multiple Models in View/Template in Django

With unsuccessful results, and now that I have been working on this for some time now I thought my next best move is to ask for help.


Pemby

unread,
Mar 22, 2016, 2:57:33 AM3/22/16
to Django users



All of the above seems to be working the way I expect it to. I can get the data I am after and pass it to a template. Now I would like take all objects, of the below model, and pass all these objects it to my '/signup.html/' IN ADDTION to what is being passed above so I can start form processing.

class Elective( models.Model ):
    elective_title
= models.CharField( max_length=100 )
    elective_description
= models.CharField( max_length=100 )    

   
def __str__(self):
       
return "{0}".format( self.elective_title )    

ludovic coues

unread,
Mar 22, 2016, 3:59:55 AM3/22/16
to django...@googlegroups.com
Have your tried to replace the view return with something similar to this line ?

return render( request, '/signup.html/', {'data': sorted(
currPersonData.items( ) ), 'eb': Elective.objects.all() }, )
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d9801311-3793-4931-a736-03d28ac50004%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42
Reply all
Reply to author
Forward
0 new messages