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!
Related
I am trying to rebuild our website in Django, and I am currently facing a series of rather difficult challenges for which I am not prepared. I was hoping to get some help in the matter.
The website I am building is an online archival platform for artists called Lekha. The core of Lekha is the dashboard page (which you can check out on this link, just sign in with the email stacko#test.com and the password test if you have the patience).
On the dashboard page, users have access to a hierarchical filesystem that we would like to function like a desktop filesystem for ease of use. Users can use the filesystem to organize their 'artworks' into folders. These artworks themselves are a collection of media, along with the metadata of the artwork. To upload this media and fill in the metadata, users will use a form. If users want to edit an artwork's metadata, they can do so through an auto-populated form on the same page.
The issue that I am facing is that I am struggling to find an elegant way to make all of this happen without reloading the page. Users need to be able to add, delete, and rename folders in the filesystem without reloading the page (just like dropbox). They also need to be able to add 'artwork' database entries using a popup form, and edit 'artworks' they have created through an auto-populated form that is also served to them without reloading the page (as it is currently implemented on our existing dashboard page).
All of the Django tutorials I have seen delete items using a dedicated /delete/ page, but this would require reloading the page. Based on my research, the solution I am looking for has to do with asynchronous updating through AJAX.
I wanted to ask all the Django experts out there what the best way to go about this would be. Are there any best practices I should know about before going into this? We are building our website to be robust and scale well.
Are there any specific libraries for asynchronous stuff in Django that are best?
How do asynchronous websites scale if we have several users on them at the same time, and should I write the backend in any specific way to account for potential scaling issues?
What is the difference between ASGI and WSGI?
Are there tools that I can use such as htmx to make my life easier?
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
I have a comments table what is linked to blog post. For each comment in the database table it is listed as it's own row on the template. I would like to lazy load each comment so users browsers don't need to load thousands of comments in their browsers.
Is this what SQLAlchemy's lazy='dynamic' does? The docs confuse me on the subject of lazy and eager loading. I am only use to using lazy load libraries to lazily load images. I am new to Python, Flask and SQLAlchemy.
If per your comment, you're loading the comments via a query with yield_per, the lazy attribute is somewhat irrelevant as it applies to entity relationship mapping (e.g. if you were loading posts which each had their own comments mapped as a relationship attribute).
Unless you're planning to stream the comments over an HTTP connection that you will keep open, I'm not sure yield_per is the best choice.
An easiest solution would be to leverage http://flask-sqlalchemy.pocoo.org/2.1/api/#flask.ext.sqlalchemy.Pagination where you initially load the first page of comments, e.g. 25. Then upon a js scrolling event, kick an ajax call kicking the same query asking for page 2, and so on.
Hope it helps.
I am using the Django template system. What I want is, when I submit a form, or click to an url link, page does not refreshes, but loads with the data returning from the server. Is it possible?
I recommend a combination of jQuery (easy, powerful, popular javascript library) and dajax/dajaxice (http://www.dajaxproject.com/). Dajax is very easy to set up and use, and jQuery is also easy to set up and use. Dajax is strictly for AJAX communications through Django. jQuery is perfect for taking a simple site and making it more fluid, intuitive, and user-friendly.
You need JavaScript to do that. What you are looking for is called AJAX (Asynchronous JavaScript and XML). Essentially, it means you use JavaScript to send a request to the server as soon as the link/button is clicked. The server returns some data to your Script, which then can be used to manipulate the HTML page, e.g. by inserting the responded data into the DOM. Since you do everything with JavaScript, no reloading of the whole page is required.
To start, read the AJAX tutorial. There are certain JavaScript libraries that make these things more simple for you (e.g. jQuery), but you really should understand how this stuff works first, since else you might get into trubble while trying to debug it.
I'm developing a web application. It's months away from completion but I would like to build a landing page to show to potential customers to explain things and gauge their interest--basically collecting their email address and if they feel like it additional information like names + addresses.
Because I'm already using Django to build my site I thought I might use another Django App to serve as this landing page. The features I need are
to display a fairly static page and potentially a series of pages,
collect emails (and additional customer data)
track their actions--e.g., they got through the first two pages but didnt fill out the final page.
Is there any pre-existing Django app that provides any of these features?
If there is not a Django app, then does anyone know of another, faster/better way than building my own app? Perhaps a pre-existing web service that you can skin and make look like your own? Maybe there's the perfect system but it's PHP?--I'm open for whatever.
Option 1: Google Sites
You can set it up very very quickly. Though your monitoring wouldn't be as detailed as you're asking for.. Still, easy and fasssst!
Option 2: bbclone
Something else that may be helpful is to set up some PHP based site (wordpress or something) and use bbclone for tracking stuff on it. I've found bbclone to be pretty intense with the reporting what everyone does - though it's been a while since I used it.
Option 3: Django Flatpages
The flatpages Django contrib app is pretty handy for making static flat pages. I'd probably just embed a Google Docs Form to collect email addresses (as that's super fast and lets you get back to real work). But this suggestion would still leave you needing to figure out how to get the level of detail you want on the stats end.
Perhaps consider Google Analytics anyway?
Regardless, I suggest you use Google Analytics with everything. That'll work with anything you do really, and for all I know, perhaps you can find a way to get the stats you're really looking for out of it.