0

How does Django access data in sqlite database?

Lets assume this scenario:

Very basic models.py, and views.py are created to accept user input from page and store it in sqlite database. If a user clicks a submit button, that data is sent to sqlite database, and the user can view it by accessing the Django Admin page. Later the user wants to display the stored data using CBV in views.py:

class Some_ListView(ListView):
    template_name = 'some_list.html'
    context_object_name = 'SomeInfo'
    model = models.SomeModel

And the data in the database is rendered in HTML like this:

{% for item in SomeInfo %}
  <p>item<p>
{% endfor %}

When & how does Django load data when rendering a page? Does it access & retrieve data whenever a any html page is loaded? Is there any way to load data in a previous page, temporarily store it, and inject it in a future page? How do you get connection? Does it use connection pool?

I will be using PostgreSQL for database for my real project, and I'm looking for advice on when & how to establish connection using connection pool.

Another scenario:

Some data were prepared by creating Class WellInfo in models.py. You can view this data in Django Admin page:

enter image description here

Using these data, I was able to make a list page using ListView like this:

enter image description here

Upon clicking any of the row, user will be navigated to a page that renders that object instance's data. So if a user clicks University Well, user will reach a page that has data relevant only to University Well. And I did so without explicitly telling Django to use only that object. I only set up primary key url using api. How did Django know to display only University Well's data, but not the others?

Eric Kim
  • 2,493
  • 6
  • 33
  • 69
  • pass the data item as a dictionary to the template through view.py and use the keys inside **{{ }}** to get its value – Abhijith Konnayil Jul 21 '18 at 21:41
  • These sources may help you. https://docs.djangoproject.com/en/2.0/ref/databases/ https://stackoverflow.com/questions/26470158/django-1-7-and-connection-pooling-to-postgresql https://devcenter.heroku.com/articles/python-concurrency-and-database-connections I currently use a Django Multi-tenant application with Postgres on Heroku. Heroku has its only limitations in terms of connections with postgres. I have yet to see a negative effect. – Oh Great One Jul 22 '18 at 00:52

0 Answers0