I use the Django admin "change list" page to monitor the state of objects created and updated by my site's users. I find myself frequently refreshing the page as my users do their work. Is there a good way to make it automatically refresh, so that I can see new changes within a few seconds? Looking for a technique that is easily reusable across my project's many "change list" pages.
For an easy implantation you could set up a button to only refresh the changelist through an ajax call instead of the whole page (though setting it up with a view would be easier than changing the built in admin site without breaking anything). You can also set that function to refresh on a given interval.
Another option is to look at implementing Comet technology such as a push server. It would load new entries whenever they are added (like how Facebook does). It's rather complicated, but if you're up to it it might serve your purpose
Related
I would like to make a web application where a user can:
click a button on a webpage to create a **random video (with moviepy)
play that video back on the webpage
click the button again to create a new **random video that replaces the existing one
** The random video will be created with a python/moviepy script that downloads a bunch of random video clips from the internet to a directory on my computer. It then compiles them into one video (.mp4 file).
I've done the python script bit already which successfully creates the video file.
To make the web app bit I have been recommended django and that's where I'm stuck!
So far I have installed django and got to grips with the basics .. I have a home page that says "Hello world".
My question is where do I go from here?
How do I connect my python/moviepy script with django?
What steps, apps, components etc, within django should I look into to make this thing?
I'm new to coding and looking for some guidance.
Thanks!
If you create a model with FileField then your moviepy script should upload videos into that field, that field can save the video in a specified directory in MEDIA_ROOT(you can store your post based on date) then that field will store the URL to it (you need to specify MEDIA_URL in settings.py). You can define some sort of IDs to them, if video privacy is not important, then you can use the model IDs. These IDs can be obtained trough Path converters .
At the client-side, javascript is needed. Simply running the script in view is possible, but then the user will need to wait for the response (and the browser should run into time-out).
You should look at server-sent events. With Vue.js you can easily display a loading element while waiting for the event (a video to be generated) and then download and switch to the video (see Django CRUD application tutorials). The python script can run asynchronously (you call it in the view).
It is a lot, I know.
Actually I'm going to learn these now, sorry for mistakes.
As I see, Django is for complex sites, you should look for Flask instead.
(I am learning Django and I know nothing about Flask, so I`ll go with it)
Here is the needed setup:
define the urlpatterns for handling the URLs
create a model for storing your video
create a django template for your page (html)
define a view for rendering out the template (passing the video)
maybe some css to design it
You can run your video generator(in the view) at every reload and override existing video(in this case you don't even need a model) or you can save the generated videos and capture the IDs in the URLs (for example: https://yoursite.com/1), in this case, the videos remain shareable.
If you go with the first option and sharing videos is not important for you then you could write a simple html page with a video and a button. The button can trigger a javascript function to run the video generator python script and refresh the page(the video is overridden), you may need to wait to the script otherwise the old video can load.
Restful API is a more advanced way to refresh the video, without reloading the page.
After these, you can deploy your page using an Apache server for example.
I tried to give you some guidelines (I am learning this on my own)
Hope that it helped :) There are tutorials for these.
i have a database that store some rows that are generated every second. I would like to update website every new content arrives to db, and i don't want to refresh page, but add new rows to current page. What is the best approach to this?
You should use AJAX queries, either of two techniques:
Periodically have the page request updates (and update the table with the result in JavaScript; backend remains plain django); or
Use long-polling, a technique commonly known as "comet", for keeping a connection open with the server and receiving a server event when there is an update. The backend for this could be a bit tricky in frameworks designed for request/response pattern; but you can find leads on how to do it in python here.
I am writing a CGI script in python 2.5 and I have run into an issue I cant solve.
My cgi script allows a user to into data into a html form and press refresh and the data gets successfully added. But it has turned out that if a user presses the refresh button on their browser the data that they inputted before gets added again. This is not the behaviour that I am looking for, and as such I would love to be able to redirect a user after a HTTP POST/GET request back to the main page.
This is of course naturally possible, by using the :
print "Location: www.website.here"
line in python. But I cannot do this as I need to be able to store messages between each refresh in order to be able to display information to the user.
These messages are essentially validation error messages, and if a user inputs wrong data and presses submit then my page should reload and print the error messages. I have this side of things working, but if I need to redirect using the above line, then I loose this.
So my question is are there any other possible ways to remove the option to refresh a html page using CGI and python, or am I looking at this all wrong and should I be trying to find a way to store my messages after a HTML redirect?
I assume you actually have two scenarios:
The user entered the data correctly; reloading the page would cause the added data to be added again (which is wrong); the user may be redirected to the start page.
The user did not enter the data correctly (there are error messages); reloading would cause the same message to be displayed again; redirect unacceptable.
I suppose you can issue a redirect only if the data validates, and display error messages in the other case.
If you have an option to migrate your CGI script to a modern web framework like Flask, Bottle, Django or Pyramid, I suggest you do so. This will give you session state and generally more convenient and modern environment.
A common way to do this is to store the pending messages for a given user on the server, associated with that user's session, and to display them the next time you deliver a page to that user.
Such messages are often called "Flash messages" (nothing to do with Adobe Flash) or "Session messages".
See the Django documentation for an idea of how you would do this in Django.
Your question is extremely confusing, because you say “press refresh” adds data. – You also don’t mention if your form is submitted via POST or GET (is it submitted at all??).
There is nothing that protects you from double entries (the user can always hit reload) if you don’t guard actively against them. One way is to deliver your form with some unique number and check if you already accepted submissions for that number.
And the other problem you face is as old as HTTP itself: session state. If you don’t want to start with session IDs or cookies, but need to carry your data through some pages, add all state to the URL all the time.
The new approach is to use AJAX, where your submit button is not going to submit classically anymore, but instead is going to execute some JavaScript in which you submit on a different (logical) connection, wait for the result and than change something in the still existing HTML page so that the user can see the result (e.g. adding a paragraph at a prominent place), but has the form still in front of him as is.
I am building a website for a comedy group which uses Facebook as one of their marketing platforms; one of the requirements for the new site is to display all of their Facebook events on a calendar.
Currently, I am just trying to put together a Python script which can pull some data from my own Facebook account, like a list of all my friends. I presume once I can accomplish this I can move to pulling more complicated data out of my clients account (since they have given me access to their account).
I have looked at many of the posts here, and also went through the Facebook API documentation, including Facebook Connect, but am really beating my head against the wall. Everything I have read seems like overkill, as it involves setting up a good deal of infrastructure to allow my app to set up connections to any arbitrary user's account (who authorizes me). Shouldn't it be much simpler, given I only ever need to access 1 account?
I cannot find a way to retrieve data without having to display the Facebook login window. I have a script which will retrieve all my friends, but it includes a redirect where I have to physically log myself in to Facebook.
Would appreciate any advice or links, I just feel like I must be missing something simple.
Thank you!
Just posting up my notes on the successful advice, should others find this post;
Per Daniel and William's advice, I obtained the right permissions using the Connect options. From William, this link explains how the Facebook connection works
https://developers.facebook.com/docs/authentication/
This section on setting up the actual authentication was most helpful to me.
http://developers.facebook.com/docs/api
Basically, it goes as follows:
Post a link to the following URL. A user will need to physically click on it (even if that user is just you, the site admin).
https://graph.facebook.com/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=http://www.example.com/HANDLER
This will redirect to a Facebook login, which will return to http://www.example.com/HANDLER after the user authenticates. If you wish to do more than basic reads and news feed updates you will need to include this variable in the above link: scope=offline_access,user_photos. The scope variable just includes a comma separated list of values, which Facebook will explicitly tell the authenticating user about during the login process, and they will have to OK. Most helpful for me was the offline_access flag (user_photos lets you get at their photos too), so I can pull content without someone logging in regularly (so long as I store the access token obtained later)
Have a script located at http://www.example.com/HANDLER that will take a variable from the request (so facebook will redirect to http://www.example.com/HANDLER&code=YOUR_CODE after authentication). Your handler needs to pull out the code variable, and then send the following request:
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_CLIENT_ID&
redirect_uri=http://www.example.com/oauth_redirect&
client_secret=YOUR_SECRET_KEY&
code=YOUR_CODE
This request will return a string of the form access_token=YOUR_ACCESS_TOKEN.
Just parse off the 'access_token=', and you will have a token that you can use to access the facebook graph API, in requests like
http://graph.facebook.com/me/friends?access_token=YOUR_ACCESS_TOKEN
This will return a JSON object containing all of your friends
Hope this saves someone else some not fun time straining through documentation. Thanks for the help!
It is true, that Facebook's API is targeted at developers who are creating apps that will be used by many users.
Thankfully, the new Graph API is much simpler to use than its predecessor, and shouldn't be terribly difficult for you to work with without using or creating a lot of underlying infrastructure.
You will need to implement authorization, but this is not difficult, and as long as you prompt the user for the offline_access permission, it'll only need to be done once.
The documentation on Desktop Authentication would probably be most relevant to you at this point, though you might want to move to the javascript-based authentication once you've got a web app up and running.
Once the authentication is done, all you're doing is making GET requests to various urls and working with the resulting JSON.
Here's the documentation about Events, and you can get a list of friends from the friends connection of a User.
I'm not expert on Facebook/Facebook Connect, however I've seen it used/used applications with it and it seems there's really only the 'official' way to do it. I'm afraid it looks like your best bet would probably be something along the lines of this.
http://wiki.developers.facebook.com/index.php/Connect/Authentication_and_Authorization
Regardless of how you actually 'use' it, you'll still need to authorize the application to connect to the account and this means having a Facebook App as well.
The answer to Facebook application authentication is hard to find but is actually found within the "Analytics" page of the Graph API.
Specify the following: https://graph.facebook.com/oauth/access_token?client_cred&client_id=yourappid&client_secret=yourappsecret , you will then be given an access_token that you may use on all other calls.
The Facebook provided APIs do NOT currently provide this level of functionality.
As far as I know, for a new request coming from a webapp, you need to reload the page to process and respond to that request.
For example, if you want to show a comment on a post, you need to reload the page, process the comment, and then show it. What I want, however, is I want to be able to add comments (something like facebook, where the comment gets added and shown without having to reload the whole page, for example) without having to reload the web-page. Is it possible to do with only Django and Python with no Javascript/AJAX knowledge?
I have heard it's possible with AJAX (I don't know how), but I was wondering if it was possible to do with Django.
Thanks,
You want to do that with out any client side code (javascript and ajax are just examples) and with out reloading your page (or at least part of it)?
If that is your question, then the answer unfortunately is you can't. You need to either have client side code or reload your page.
Think about it, once the client get's the page it will not change unless
The client requests the same page from the server and the server returns and updated one
the page has some client side code (eg: javascript) that updates the page.
You definitely want to use AJAX. Which means the client will need to run some javascript code.
If you don't want to learn javascript you can always try something like pyjamas. You can check out an example of it's HttpRequest here
But I always feel that using straight javascript via a library (like jQuery) is easier to understand than trying to force one language into another one.
To do it right, ajax would be the way to go BUT in a limited sense you can achieve the same thing by using a iframe, iframe is like another page embedded inside main page, so instead of refreshing whole page you may just refresh the inner iframe page and that may give the same effect.
More about iframe patterns you can read at
http://ajaxpatterns.org/IFrame_Call
Maybe a few iFrames and some Comet/long-polling? Have the comment submission in an iFrame (so the whole page doesn't reload), and then show the result in the long-polled iFrame...
Having said that, it's a pretty bad design idea, and you probably don't want to be doing this. AJAX/JavaScript is pretty much the way to go for things like this.
I have heard it's possible with AJAX...but I was
wondering if it was possible to do
with Django.
There's no reason you can't use both - specifically, AJAX within a Django web application. Django provides your organization and framework needs (and a page that will respond to AJAX requests) and then use some JavaScript on the client side to make AJAX calls to your Django-backed page that will respond correctly.
I suggest you go find a basic jQuery tutorial which should explain enough basic JavaScript to get this working.