Javascript in Python

Hi. I am writing a simple web page. I have a static page which uses Javascript to validate a registration form. I then call a Python script to access a gadfly DB which checks if whether the username already exists.
I need to some how get python to return to the same registration page if the username already exists. When it returns I need it to use the Javascript alert() popup to display an error msg. Else if the username isnt already in use, I will create a dynamic page.
I understand that this is a very inefficient way of doing things and it probably wouldve have been better to create the pages dynamically and usse python to validate the form. But, this is the way it must be done.
Is there a way of getting Python to return and use a Javascript popup??
Ideally, I would have the script just return false or something.. so that the page doesnt need to be refreshed.
Any ideas??
Thanks heaps.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

EOLConnect With a Mentor Commented:
you can use a script like this for making redircects. say you store it in your cgi-bin as redirect, then it works like.

ok, here's the script. Be sure to set executable permissions.

#! /usr/bin/python
import cgi

form = cgi.FieldStorage()
url = form['url'].value

print 'content-type: text/html'
print 'location: %s\n'%url
print '''<HTML>
         <META http-equiv="Refresh" content="0;url=%(url)s">
         <BODY onload="try {self.location.href='%(url)s' } catch(e) {}"><a
The simple answer is no. You can not validate a username against a server-side database without dynamic web pages. You can also not do any client-side python scripting ( altough that would be very nice ) and java-script can not portably connect by itself to external datasources.
Btw. without dynamic pages, what's the use of a login anyway? If you just want to restrict access to an area, use htaccess ( can also bind against a db )
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

waulsyAuthor Commented:
Hey, thanx for the comment.
I knew it wouldnt be easy. We have been told to use Javascript for the validation of a registration form. Then we need to somehow store the registration data (either textfile or Db), and dynamically create a page on user login.
The problem being, that is going to be hard to keep consistency about the error messages, because validation is required to be done with Javascript (e.g input boxes). And I am assuming I will need to use Python (e.g. store input data, and validate the stored data against the input data). Unless it is possible to create textfiles with Javascript? And also then read in from them.
Any advice would be greatly appreciated.
waulsyAuthor Commented:
I am thinking I mite need to recreate the static pages dynamically when the login details are incorrect and get a Javascript alert to popup on page load( is that possible?). Doesnt make sense to do it that way I know. But we need to submit some static pages.
You can insert a JavaScript alert on page load. However, I don't understand how you're using JavaScript to validate the registration, unless the JavaScript is being run on the server.

Python is an excellent CGI language, however. Are you sure you can't just submit the page to a Python CGI script on the server?
waulsyAuthor Commented:
Hi rjkimble,
Yeh I cant just submit the page to python script. We must use Javascript to do some validation. I am only using javascript to make sure certain fields arent empty or invalid etc, not to check against the db.
The reason i was asking was becuase, with the current situation, it is impossible to keep consistency about the error messages.
Perhaps you could use Python to generate a JavaScript file on the server and include that file in your HTML. That might help you keep your error messages consistent. Is that the kind of idea you're looking for?
waulsyAuthor Commented:
Yeh I suppose that is what I am looking for. Is it possible to tell Python to return to a static page? Because I still need Python to validate login details, but then wanted a Javascript popup if values are wrong. Am I asking the impossible?
rjkimbleConnect With a Mentor Commented:
You could post your login credentials to a Python CGI script, which would validate them. If the validation fails, you could redirect to a suitable static page; and if the validation succeeds, you could redirect to the first page for logged in users. I'm not really sure how your site is structured. There a lot of possibilities, but if you're trying to serve up a big web site, it could get kind of messy.

It would be good to check out the Python cgi module.
waulsyAuthor Commented:
Okay that sounds good. The site is only small, so I dont need to worry to much.
Are you able to tell me how to redirect a to a static page in Python. Then we can end this forum.
Thanks for all your help.
Also, that static page would be able to contain Javascript rite?
btw, this script represents already a dynamic web-page
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.