I got one page. On the page is a contact form. After clicking the 'send' button I want to change the form containing div to 'thank you for mailing us' div. And stay at the same 'id' on the page.
One way to solve it is to put an another template and load it as 'success url' But this way the whole page is being loaded again and the user lands on top of the page without seeing the confirmation.
Is there a way not to load the page again and to change the div somehow? With jQuery for example? How to do it? Is there even an another way?
Would be great to have some hints. Many thanks in advance!
Using Javascript is the easiest solution. Here is example for using AJAX with forms. Django can detect AJAX requests out of the box via request.is_ajax().
Another option would be to set cookie, redirect to the same url and handle the response differently (depending on given cookie), but that isn't elegant at all.
Related
I have an endpoint that renders a template that has a list of movies
On the page, there is a button called "Add movie" and an input field to add the name of the movie. When the button is pressed, I want to add a movie of the name to the list of movies and re render the page.
Right now, I am doing it all in JS, with a fetch request, but I can't re render the page like this.
How could I achieve the effect I want?
You can update the page without reloading/re-rendering the whole webpage using AJAX calls and changing a specific element inside the HTML's DOM. You can do this in raw Javascript or use some JS libraries to simplify the code. Here are some libraries you can consider if you've a large'ish project and such requirements might come up again:
jQuery
intercoolerjs.org
unpoly.js
You're rendering on the server-side, so in order to re-render, you need to send data back to the server. One way of doing this creating a form element which posts data to the same page when you click the button. And then on the server side, take the inputted movie, add it to the other movies, and then you can re-render and return.
This answer attempts to provide general guidance, since your question was general.
I need help with python...
Do you know how I can check response of web browser after clicking button login (here: submit)?
I want to compare html code and return True if my login will be a successful but unfortunately I don't know how. :/ Any hint would be priceless. :)
That's my code from selenium:
driver.find_element_by_css_selector("input.username").send_keys("margie")
driver.find_element_by_css_selector("input.password").clear()
driver.find_element_by_css_selector("input.password").send_keys("margie")
driver.find_element_by_css_selector("div.btn.submit").click()
Can I use "if"?
Thank you for your time guys!
You can just continue using driver after clicking on submit button. For example, driver.page_source would contain the html code of the page displayed after the login.
There is no universal silver bullet to check if the login was successful or not. It depends on the web-site you are testing against: it may redirect to a particular url, have certain elements on the web page, certain title etc.
Just a side note.
If you would follow Page Object pattern/concept, you would have a separate object for Login Page and a separate object for the page displayed after the login aka Home Page - like in this example. The actual page look check after the login would be incapsulated inside the Home Page object realization which would make things clearer and better organized.
See also:
How to assert in selenium test case for login success?
Page Objects in Python
In Django whenever I have to use site navigation I use the Reverse resolution of URLs. This way I can make django -render- each new html page and pass that page whatever arguments I want to through the views.
However I am wondering how should I do this in web.py. For instance, I have a web.py template that contains a variable $user. At some point in the main webpage a simple button contains a link of the form
Account
which redirects a user to his account page. Now, I need to pass $user on to account.html so that he/she can change his/her details. The problem is that since I can't directly link to account.html cause it's not a static page, how should I go through web.py and use its render method?
Thanks in advance.
It seems to me that the best way to do this would be with a session variable. That kind of variable can be consistent across several pages.
I have a html file with a form:
<form method="get" enctype="multipart/form-data" action="bin/a_python_script.cgi">
(I 'have to' use method="get", because when I use method="post" I get weird 'Page expired' problems with the 'back' button in Internet Explorer 8)
The resulting page shows this in the address bar:
http://www.mydomain.com/bin/a_python_script.cgi?var1=a+variable&var2=another+variable&Submit=submit+this
My preferred output in the address bar would be:
http://www.mydomain.com/bin/a_python_script.html
How do I hide everything after ".cgi?" ?
And can I change ".cgi" into ".html" ?
Unfortunately, there are no options other than changing the form method from 'get' to 'post'.
GET encodes all of the form fields into the URL. There's no way to hide anything without removing the data.
A quick Google search for "prevent page expired message" turns up quite a few results (the examples are mostly PHP, but the concept could be transferred to any language):
Google - prevent page expired message
Your form method must be "post" and not "get".
In your cgi script, you should be able to retreive post data.
for the extension change, with a .htaccess it should works.
Leave the form as a POST, then use the post-redirect-get pattern to allow the user to go back or refresh without a warning.
Agree with Justin Niessner - however, after the hit you could do redirect to another page, so that the parameters will not remain in the URL. Or, you could submit the form via ajax in the background, so the user won't notice the params sent
I'm trying to login to a login.live.com, but Mechanize will not recognize that there is a form on that page. Does anyone have any suggestions?
br=mechanize.Browser()
br.open('https://login.live.com/')
br.select_form(nr=0)
This results in:
mechanize._mechanize.FormNotFoundError: no form matching nr 0
When there clearly is a form on that page.
Try getting the form by name instead with br.select_form(name="f1").
(I got the form name from the page source - I assume you want the login form.)
The problem is that the form is not in the html that is delivered as a response to the HTTP GET request. It's being created later a a result of a javascript script being executed in the browser.
For more information about how to get content that has been dynamically generated, please have a look at the answers to this question.
r = br.open('https://login.live.com/')
r.get_data()
# outputs:
# ...
# Windows Live ID requires JavaScript to sign in. This web browser either does not support JavaScript, or scripts are being blocked
# ...
I found no workaround, and am suggesting you to use Selenium/webdriver instead.