I'm making a small app for internal use between 3 people. the idea is to have a page where user can upload files and data ( more specifically images.) to the database and a second page where the information the user uploaded in the first page will be visible without having to manually refresh the page. I have read about comet and ajax and I think having a function check the Db every certain time is not what I'm looking for. Since there will be almost no updates in the DB. maybe every 3 to 4 months the user might update a new image.
Have a read through at least one good tutorial on ajax & django, it's quite simple once you get started with it. But you can't really achieve this without AJAX.
Take a look at this;
https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html
Related
I've been building some simple websites with django and am somewhat comfortable with it, enough to use models, templates and apps at least.
Now, I'm making a website for a friends small business that will have shopping cart functionality, and will display inventory. He doesn't have anything in stock and get's it all from wholesalers, so inventory will be shown directly from wholesalers database who have an API.
Everything I could find when searching for a tutorial covered creating an API, not using one.
Do I have to have a mode or a database locally? Or is everything taken from the API as a series of get requests?
How do I pull say, all cars that are yellow, or all that are yellow made by BMW, and display on my inventory page with pagination?
Not asking for a specific set of instructions here, just a very high level overview so I know what to search for to teach myself how to do this.
consuming an api is not a django related, it 's python related. An easy way to consume an api is python's requests library.
Regarding your question on how to handle the data. Both of your ways are possible with some advantages and disadvantages. If you always get the data from the api, they will be always up to date but the load time is longer. If you download all data first to you local database, you need to rebuild their datastructure but loading time it faster and you can use the power of the django orm.
A third option might be that you query the product data from the frontend via javascript so that you just have to build views to handle action such as "add to shopping basked" or "checkout".
Like I said in the title, I need to know how can I allow access to the objects from one model of a Django app from another Django app.
For example, I have a Django App on one VPS, and the other on another VPS. In the first one, I publish daily news about science. I like to put a div in the other Django app landing page that shows the news that I publish in the first app.
I know that I didn't put any code, but this is because I don't know how to start. I need the first push to help me figure how to make this and then I'm pretty sure that I can come back to make specific questions.
What I have been trying to find, with no answer yet, is how I could have a user click on link to a template, then instead of waiting for the whole page to load, allow the user to view what has already loaded while they wait for more load heavy content to arrive, similar to what Facebook does when you first get to a page and see things loading.
There is not much more I can say as the question is pretty self-explanatory. I have checked google and stack overflow.
In order to allow the use to view the loaded page and dynamically view the rest of the page, you use a technology known as AJAX. It allows you to make asynchronous calls to the database, which can be triggered by some JS event(like onscroll) and load the queried data without reloading the entire page.
AJAX in Django is pretty straightforward, though some knowledge of JQuery(or even Javascript) will be required. You may also use the python package django-dajax which will make things easier. I think you will find the following links useful:
Tango with Django ajax guide (one of the best, but a bit tough)
django-dajax docs
Hope this helps!
I was reading some blogs and came up to the conclusion that django is an MVT architecture which do not maintain state. I am working on application that have maps visualization. When the user selects a variable from the drop down, a request is sent to the backend database and on the screen, it generates the heat-map of that specific variable.
what I want to achieve is that if I go some other tab and do some other changes like change the layer of map or select some another variable, the state of old heat map should be maintained regardless if I want to clear it. I do not know how to maintain state in django can anyone help me in this?
You can leverage the Django's sessions mechanism. You'll need an AJAX request that POSTs the current tab state to Django, so your backend code can restore it on page reload.
My django app needs to display data on my homepage which it collected from third party. Requesting the information and waiting for response takes about a second, which is too long processing time for a homepage. The data which my app receives doesn't change often, so there is no reason to fetch that data every time homepage is being rendered. Instead, I want to retain the data and have my app make a request only if the last "refresh" has been done more than an hour ago.
Since using global variables in django is apparently a no-no, I'd need to make a database model which will at all times hold a single record. This feels wrong. Is making a one-record table really a way to go here?
Instead of creating a model to cache the remote site's response, you can use Django's caching framework. More specifically, you can cache a specific view and set a timeout for the cached view. See this documentation page for more details on how to do that.