Execution of python script on webserver via link - python

I finally managed to get my web-server up and running python.
I am now able to enter the direct url pointing to the python script and it is being executed.
I wrote a script which generated a html page and returns it - working; and if string is saved as html and opened, it is displayed exactly how I want it.
However, I want this page to be displayed if a user navigates to the "Database" link which is displayed in the navigation menu
<nav>
<ul class="main-nav">
**<li>Database</li>**
<li>About</li>
<li>Admin</li>
</ul>
</nav>
I would like the script to be executed when the user clicks on the menu link but I do not want the user to see the actual link to the python script.
Ie the link should display localhost/database.html but the script should be executed and whatever is returned should be displayed.
I hope this makes sense - any help is appreciated

The easiest way to achieve what you want (the end user not seeing that its not a HTML file) is to move the file into a folder and configure the server to execute and server the results of that file when the user navigates to that folder, i.e. move the file into a folder called:
/database
and then change your link to point there, then configure the webserver to accept the python filename as a default file, in your case running apache it should be something like this:
Find the DirectoryIndex directive in your Apache configuration file (httpd.conf) or add it to a .htaccess file and change it to look like this if you want to limit your default index file to just index.html:
DirectoryIndex index.html
You can also include more resources and they will be used in the order given, e.g
DirectoryIndex index.html index.php
would display the index.html file first if both index.html and index.php existed.
Don't forget to restart Apache if you made the change to the httpd.conf file.
this way the url that you would see when testing locally would be:
localhost/database
and avoids putting any extension on it, this is simpler that trying to do some kind of redirection.
personally when using pages that do something like perform a query i tend to call them process to indicate that they actually do something when i'm working on it, and static files i tent to call index (unless there are multiple files in that location) this isn't necessary, just my personal preference, but it helps keep the configuration simple as there are then only so many names you have to add to the default list.

Related

Accessing app-specific server-side generated files in django template

I have a django app (my_app) that based on the user query:
creates a file from the db
runs a program on the file from step-1 and gets an output file
generates a json file from the step-2 output file
renders a D3 visualization from a django template using the data from the json file from step-3
I need the program to run on the server side and the json file to be generated server-side as well.
Because the json files are query-specific, I thought it's not a good idea to keep these files in the /static/ folder and thought of keeping the files (even if temporarily) in e.g. /myapp/output_files/ folder.
The problem is that there is no url pattern corresponding to /myapp/output_files/my_file.json and I get a "Page not found (404)" error if I try to open the generated file and it obviously doesn't load in the javascript code in the template.
Is there a better way to design the system?
If the design is ok, how can I access a json file in the app's folder from the django template? Do I need something in the urls.py?
P.S. Everything works fine if I change the json location to /static/ or its subfolder.
Just add the location to your STATICFILES_DIRS setting as shown here
However, you probably need to build a view function that can somehow return the json based on some parameter in the url. Static files are meant to stay static...

How to pass directory path from a template to a view?

I'm pretty new to Python\Django.
I'm trying to make a local Netflix-like Library for my Movie\TV shows collection.
The general idea is that the user chooses a media folder, the server side runs on the files in the folder adds them to the database and then the user can search for the items and play them back in the GUI.
The first snag I ran into is getting the folder path from the user without actually uploading any files. After doing some searching online I found this :
<input type="file" id="file_input" webkitdirectory="" directory="">
This HTML code allows the user to choose a folder and iterates through all the files inside, however, I don't know how can I pass this information to views.py so that I could run logic on the input.
Does anyone know how this could be accomplished ?
For security reasons, browsers do not allow to get folder path (Stack Overflow). Since you do not want the user to upload files, the possible solution would be to explicitly mention the folder path in <input type="text">. The simplest solution would be python –m SimpleHTTPServer (source, docs). You may be also interested in this Django app.

What is the best way in python/django to let users add files to the database?

I am trying to let users create html pages that they can view on my website. So the homepage would just have a place for file upload, then some script on my site would take the file and convert it into the html page and place it at mysite.com/23klj4d(identifying file name). From my understanding, this would mean that the urls.py file gets updated to route that url to display the html page of the file. Is it possible to let users do this? Where would I put that conversion script?
You could simply write static files to disc, and then serve them as static files. That would be easier, but it depends on your other requirements.
But, from what I understand in your question you'd need:
A form to upload
A upload handler itself which inserts into the db
A view that renders based on a path
Not sure about the urls.py entry. You'll want something in there to separate this content from the rest of your site, and you'll probably also want something to safeguard against the file extensions that you serve there.
note: this has security hole written all over it. I would be super careful in how you test this.

URL rewriting question

I have a CGI script (pwyky) that I called index.cgi, put in directory wiki/, and setup Apache to call localhost/wiki/index.cgi when I access localhost/wiki.
I'm getting errors when I'm trying to use this application -- it creates a page with links like "http://localhost/wiki/#edit/index", but when I click that link, Apace is trying to serve "wiki/#edit/index" as a file. I suspect that I need to get Apache to pass /#edit/index into index.cgi.
In particular, looking through index.cgi, its looking for strings like "#edit" in REQUEST_URI environment variable.
Any idea how to fix this?
You'd need to show your apache configuration to say for certain, but it seems that Apache isn't actually using mod_cgi to serve the index.cgi script. In your configuration there should be something like 'LoadModule mod_cgi'. It should be uncommented (i.e., it shouldn't have a '#' at the beginning of the line).
If you want to test this, then write a 'Hello World' cgi script and put it (temporarily) in place of index.cgi and see if you can get that to run. Let us know the results.
I found the problem, it turned out this is done through RewriteEngine. Pwyky puts .htaccess file in the directory with all the settings for RewriteEngine, but because AllowOverride is "None" by default on MacOS, they were ignored. The solution was to change all "AllowOverride" directives to "All"

How do i output a dynamically generated web page to a .html page instead of .py cgi page?

So ive just started learning python on WAMP, ive got the results of a html form using cgi, and successfully performed a database search with mysqldb. I can return the results to a page that ends with .py by using print statements in the python cgi code, but i want to create a webpage that's .html and have that returned to the user, and/or keep them on the same webaddress when the database search results return.
thanks
paul
edit: to clarify on my local machine, i see /localhost/search.html in the address bar i submit the html form, and receive a results page at /localhost/cgi-bin/searchresults.py. i want to see the results on /localhost/results.html or /localhost/search.html. if this was on a public server im ASSUMING it would return .../cgi-bin/searchresults.py, the last time i saw /cgi-bin/ directories was in the 90s in a url. ive glanced at addhandler, as david suggested, im not sure if thats what i want.
edit: thanks all of you for your input, yep without using frameworks, mod_rewrite seems the way to go, but having looked at that, I decided to save myself the trouble and go with django with mod_wsgi, mainly because of the size of its userbase and amount of docs. i might switch to a lighter/more customisable framework, once ive got the basics
First, I'd suggest that you remember that URLs are URLs and that file extensions don't matter, and that you should just leave it.
If that isn't enough, then remember that URLs are URLs and that file extensions don't matter — and configure Apache to use a different rule to determine that is a CGI program rather than a static file to be served up as is. You can use AddHandler to add a handler for files on the hard disk with a .html extension.
Alternatively, you could use mod_rewrite to tell Apache that …/foo.html means …/foo.py
Finally, I'd suggest that if you do muck around with what URLs look like, that you remove any sign of something that looks like a file extension (so that …/foo is requested rather then …/foo.anything).
As for keeping the user on the same address for results as for the request … that is just a matter of having the program output the basic page without results if it doesn't get the query string parameters that indicate a search term had been passed.

Categories

Resources