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
Related
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.
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'd like to modify a wiki page (Confluence by Atlassian - JIRA editors) programmatically (in python). What I tried so far is to simulate user behaviour:
click on Edit button
change content of a textarea input
submit changes with Save button
Part 1 is ok since I have the URL corresponding to an edit of the page, part 2 (retrieval of the page and modification) is ok too, but I don't know how to achieve step 3... I'm using urllib2.
Thanks for your help !!
EDIT: XML-RPC is indeed the solution, this example does exactly what I want !
# write to a confluence page
import xmlrpclib
CONFLUENCE_URL = "https://intranet.example.com/confluence/rpc/xmlrpc"
CONFLUENCE_LOGIN = "a confluence username here"
CONFLUENCE_PASSWORD = "confluence pwd for username"
# get this from the page url while editing
# e.g. ../editpage.action?pageId=132350005 <-- here
PAGE_ID = "132350005"
client = xmlrpclib.Server(CONFLUENCE_URL, verbose = 0)
auth_token = client.confluence2.login(CONFLUENCE_LOGIN, CONFLUENCE_PASSWORD)
page = client.confluence2.getPage(auth_token, PAGE_ID)
# and write the new contents
page['content'] = "!!!your content here!!!"
result = client.confluence2.storePage(auth_token, page)
client.confluence2.logout(auth_token)
Note that confluence modifies your html code when you do this. It strips out scripts, styles and sometimes title attributes on elements for example. In order to get that stuff back in you then need to use their macro code.
Easiest way to do this is to edit the page in confluence and make it look like you want and then grab the page and do a print page['content'] to see what magical new stuff the atlassian people have decided to do to standard html.
This seems like the absolutely wrong way to go about it.
First off, Confluence has a plugin architecture which should allow you to manage content programmatically from the application itself without any kind of HTTP requests. Secondly, even if you don't want to, or can't, use the plugin API for some reason, the next obvious option is to use the SOAP/XML-RPC API.
There is no reason to actually mess with buttons and textareas unless you're trying to do some kind of end-to-end test that includes testing GUI (e.g. automated cross-browser testing).
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.
I'm trying to make a website using python with CGI. This website is very basic, all it has is login, logout and display data from a database using sqlite.
I was wondering how would I create a hyperlink to a page displaying a message returned from the database using a GET form with no submit button? The hyperlink would be something like:
www.test.com/cgi-bin/test.py?message=id
Where the id is equal to an item returned from a list (which has the id and message) from a database. So when I type into the browser say, message=23, it will display that message on a page alone or when I create a hyperlink with the query string message=43, it will display the message with ID 43.
I've looked at this tutorial (Simple URL example: get method part): http://www.tutorialspoint.com/python/python_cgi_programming.htm
But I don't know how I would get rid of the submit button with the values being from the database and appended to the URL. (test.py?foo=bar)
Hopefully I have explained everything clearly.
A URL is a URL. It doesn't matter if you generate it by typing it in, clicking a link, or submitting a form (with method=GET).
…
Update based on the understanding that the question is actually:
How do I read the query string in a CGI script written in Python?
import cgi
form = cgi.FieldStorage()
messageid = form.getvalue("message")