We have a page which is dynamically generated after a few queries in the database. There are some links that when they are clicked by the user, update some information on the database but they change nothing on the webpage (or the display a discrete message).
How could we stay on the same page without re-rendering it?
Ideally, the corresponding view.py would process the queries and update the database but not the webpage.
You can send and receive your own XMLHttpRequest, but it is too much of works to do and IE will create a lot of problems.
Have you ever heard about jQuery? I strongly recommend you take a look at it and learn how to send and receive Ajax request using it.
You need to make an AJAX call back to the server with the user's actions, and process it on the server. You probably want a different view to process the AJAX request -- you could do it with the same view, but it would be somewhat silly to. The response from that view contains data (probably as JSON) or HTML, which you display on the page with javascript. Check out jquery -- it's great for the client side.
You could accomplish this with plain Javascript and AJAX. When the user clicks on a link, use XMLHttpRequest to call view.py to process the queries.
eg. For the link: <a href="#" onclick=submitdb(); >Click me!</a>
For a tutorial on implementing AJAX (XMLHttpRequest) with Javascript, have a look here:
http://www.quirksmode.org/js/xmlhttp.html
Related
I am creating an ecommerce webapp and I want to update my admin page when user place order. After research i found out about Django signals, but it seems pretty confusing. The idea behind this is that, when a user places an order I want the admin page to be refreshed and show the latest updates. I tried ajax but javascript can only work with the current open page. Can anyone help me with usibg django signals this way?
I think you misunderstand a little, if your idea is that the admin page viewed by the user in a browser is to be refreshed. The page viewed by a site visitor is retrieved on demand of the browser. The user could refresh a page, or a page script may auto-refresh on a timer (this is not a very good solution, but it is easy).
Django can't make the browser update the page.
Signals can cause django to do something in the backend, but they can't solve the problem that the browser is in charge.
There is one technology designed to allow the server to push content to the browser: websockets.
If you use websockets (Django's fairly recent built-in support is called Channels (https://channels.readthedocs.io/en/stable/)), then you can push content to the browser, where a receiving script on the page will do something with it. This is a very powerful technique, but there is a learning curve of some hours if you are starting from scratch.
Recently I came across this front-end library which tries to make this easy as
far as the browser goes: https://htmx.org/docs/
But you still have to deal with running a websocket server and learning how to send messages to a websocket. You will however feel like a superhero at the end of it, so there's that.
First of all, I only started using Django a week ago... so pretty new :).
I have a one page website. At the bottom there is a contact form. I am using different components for the website, so for the contact form I have 'contact.html' which is loaded into index.html via {& include ... &}. When someone sends a message via contact form, after click send, the user returns to the same page but with a thank you message instead of the contact form (see screenshot).
The issue is that I need to 'kill' the process going on underneath because if I reload the page a message pops and if I resubmit, email gets resend again (see screenshot).
I have had a look at httpresponse but I am unsure how to replicate the same process. Anyone could help?
This is a screenshot of contact.html and views.py
Two things.
I would first consider using a front end framework such as react. (will make your life easier)
Secondly, your code seems fine, you are sending the email twice because you are submitting the form twice.
perhaps redirect the user to a different page upon submission. there is no need to reload the page yourself.
I would have a form, then a confirmation page.
Better yet, write an api, and upon response from the server simply create a popup saying success/fail etc...
hello guys hopefully you guys are willing to work with a newbie I'm working in django and handling a google map (in jscript) which upon displaying a marker I have set a link that is clickable that opens a django url passing data regarding the location of that marker. If that data does not exist in the database I have created a definition in my view that does a check and populates the database with that data if it does not exist. Now the issue here is that what ever you type in for the url parameters ends up becoming populated in the database since there is nothing to validate it against regardless of whether you click in the google map or not. Now what I was looking to do was limit this database addition strictly to when a user clicks on a google map link, what would you guys recommend?
ajax, sessions etc... ? and how to go about it an example per se?
It would be good to have more info.
As a general rule, the http GET method shouldn't be used to change data. In this case, use a POST request, probably through AJAX.
As the title implies,
I need to fetch data from certain website which need logins to use.
The login procedure might need cookies, or sessions.
Do I need QtWebkit, or can I get away with just QNetworkAccessManager?
I have no experience at both, and will start learning as I go.
So please save me a bit of time of comparing both ^^
Thank you in advance,
Evan
Edit: Having read some related answers,
I'll add some clarifications:
The website in concern does not have an API. So I will need to scrape web elements for the data myself.
Can I do that with just QNetworkAccessManager?
No, in most cases you don't need a full simulated web browser. In most cases, just performing the same web requests like a web browser would do is enough.
Try to record the web requests in your browser, using a plugin like "HTTP Live Headers" or "Firebug" in Firefox. I think Chrome provides a similar tool out of the box. These tools record the GET and POST requests done by the website when you send a form in the webpage.
Another option is to inspect the HTML code of the login page. Find the <form> tag and its fields. Put them together in a GET / POST request in your application to simulate the same form.
Remember that some pages use randomized "tokens" in their forms, some set the tokens as cookies. In such cases, you need to request the login page itself in your application first (before sending the filled in form). Both QWebView and QNetworkAccessManager have cookie support.
To sum things up, I think QWebView provides a far more elegant way to simulate user interaction with a web page. The manual way is, however, more "lightweight", as you don't need Webkit and your application might be faster (because only the HTML page is loaded, without any linked resources like images, CSS, javascript files).
QWebView as class name states is a view, so it views something (in this case web pages). If you don't need to display loaded page, then you don't need a view. QNetworkAccessManager may do the work, but you need some knowledge about HTTP protocol, and also anything about target site: how does it hande logins, what type of request you have to send to login etc.
I have a test django app.
In one page the test show the same question to all users.
I'd like that when a user answers correctly, send a signal to other active user's browser to refresh to the next question.
I have been learning about signals in django I learning work with them but I don't now how send the "refresh signal" to client browser.
I think that it can do with a javascript code that check if a certain value (actual question) change and if change reload the page but I don't know this language and the information that I find was confused.
Can anybody help me?
Many Thanks.
There is no existing way to send a event from server to browser. But you can get your web page polling your server periodically (say every 5 seconds).
The code in javascript/jquery could be like the following
setInterval(function(){
$.post("your_ajax_handler_url/is_answerd", userId, function(xhr){
if(xhr.responseText == "answered"){
location.reload(true);
}
}
}, 5000);
That is not at all what signals in Django are for. Signals in django are server side hooks that allow you perform tasks on the server when a certain even happens.
To 'send a refresh' to the browser, you need to use a server-push approach such as Comet. Alternatively you can get your clients to periodically poll the server to look for update.
Here's some links:
How to implement Server push / long polling / comet using PHP and Javascript
How do I implement basic "Long Polling"?
What you need are coment(http://en.wikipedia.org/wiki/Comet_%28programming%29) and tornado(http://www.tornadoweb.org/)