A
A
Alexander2016-03-31 12:16:43
Django
Alexander, 2016-03-31 12:16:43

How to display data from the database?

Is it possible in python to display data on the screen like in php:

print_r($result);
die();

With this logic, so that after the data from the database nothing is shown anymore.
For example, display data about the user in the form of a library (dict).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2016-03-31
@kentuck1213

If you need it for debugging, then it is better to run django-shell with the python manage.py shell command from the project directory and execute any queries in it. No extra output will bother you and "die()" will not be needed.
You can use the dir() built-in function to get a list of all class attributes. To get the values ​​of an object's attributes, use the built-in vars() function.

from django.contrib.auth.models import User
user = User.objects.first()
vars(user)

{'_password': None,
 '_state': <django.db.models.base.ModelState at 0x7871fd0>,
 'date_joined': datetime.datetime(2013, 12, 12, 6, 6, 58, tzinfo=<UTC>),
 'email': '[email protected]',
 'first_name': 'Сергей',
 'id': 1,
 'is_active': True,
 'is_staff': True,
 'is_superuser': True,
 'last_login': datetime.datetime(2016, 3, 31, 8, 1, 59, 780493, tzinfo=<UTC>),
 'last_name': 'Горностаев',
 'password': '<secret>',
 'username': 'gornostaev'}

D
DuD, 2016-03-31
@DuD

try
print $result
sys.exit(0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question