Monday, March 21, 2016

Pass these specific model objects to one template

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.


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/59859122-6816-4935-9bfa-530013ce994f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment