0

I came from CakePHP and just started playing with Django framework. In CakePHP, I have the habit of printing out all the returned array using pr() straight out to the webpage. For example:

  • A controller spits out a $result to a View, I use pr($result) and it will print out everything right on the webpage so I know how to travel through $result from my View.
  • A form POST a $request to a Controller, I use pr($request) to see what is sending in before processing it in the Controller. The content of $request will be displayed immediately on the webpage right after I hit Submit the form.

I'm wondering if I could do the same thing in django instead of going to the shell and try pprint (or could I just use pprint to print out to the web???) This is a really simple example about what I'm talking about: app_name/views.py:

def detail(request, soc_id):
    soc = get_object_or_404(Soc, pk=soc_id)
    return render(request, 'socs/detail.html', {'soc': soc})

How can I just view clearly what is in "soc". In cakephp, I could just pr($soc) right there and it will be displayed right on the detail.html page.

I have tried this and it didn't work (I'm sure it's basic but i'm just new to this)

import pprint
def detail(request, soc_id):
    soc = get_object_or_404(Soc, pk=soc_id)
    pprint.pprint(soc)
    return render(request, 'socs/detail.html', {'soc': soc})

I have spent two days doing research but I couldn't find the answer. I'm hoping one of you can help this newbie out.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
H Bui
  • 63
  • 1
  • 7

1 Answers1

0

The way you're trying to print will show the print in the terminal that is running your Django server. If you just need to see it quick, check there.

If you want to output the value on to the rendered page, you'll have to include it in the template using template tages. It looks like you send {'soc': soc} to your template as context. Because of this, you should be able to use it in your template. So, in your template (socs/detail.html), just add {{ soc }} somewhere and it should print out the value. The template has full access to the object, so if you wanted something specific, you can also print that instead (like {{ soc.id }}).

If you want to see everything that's in the object without specifying all of the different fields yourself, send OBJECT.__dir__. For example, for this you'd send {'soc': soc.__dir__} as your context. Keep in mind that this likely should not be used for anything but inspection on your part.

If you'd like to learn more about Django's templating, check out the syntax and more.

aredzko
  • 1,690
  • 14
  • 14
  • Thank you for your answer. I don't know how to format my comment so I posted an "updated answer" – H Bui Sep 20 '16 at 19:49
  • @HBui I've updated my answer. Hopefully it's what you're looking for. It won't show nested relations, so if you have other objects, they may just show whatever their `__str__()` is defined to – aredzko Sep 20 '16 at 20:28
  • The __str__ came real close to where I'm heading to. But instead of split out details of (soc) like how many records came back, which fields came back, which value goes with which field, it split out: "['prepare_database_save', 'DoesNotExist', '__class__', 'modified', '__repr__', '__ne__', '_do_update', 'save', '_check_local_fields'," which is not what I need. No field name or values in there. I hope I make sense. I really appreciate you taking your time helping me. – H Bui Sep 20 '16 at 21:02
  • @HBui I'm sorry, but I'm not aware of a way to print out what you're looking for with as little syntax as you're asking for. – aredzko Sep 20 '16 at 21:35
  • I've just updated my other "answer" hoping it could give you more information on what I need. Thank you for taking time to help me. – H Bui Sep 22 '16 at 00:53