Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSS not linking with CGI script
#1
hi,
i am new in python cgi.I try to code cgi with python.i can not linked with css file with python script.The css file name is style.css.
my code is here :

#!/usr/bin/python3.5


print("Content-type: text/html\r\n\r\n")
print("<!doctype html>")
print("<html>")
print("<head>")
print("<title>")
print("Cgi-python")
print("</title>")
print("<link rel='stylesheet' type='text/css' href='/style.css'>")
print("</head>")
print("<body>")

print("<h1>My name is zzzzzz</h1>")

print("</body>")
print("</html>")
this is my python cgi script...

here i tell you that css file locate at the same directory where python script located.
css file contain :

*{
    padding: 0px;
    margin: 0px;
    font-size: 20px;
}

h1{
    color: blue;
}
what is the problem with my script.Why i can
Reply
#2
You should really use python's docstrings, then you dont need to print each line such as

html = """Content-type: text/html

<html>
<head>
<link type="text/css" rel="stylesheet" href="/cgi-bin/styles.css" />
</head>
<body>
<select>
    {OPTIONS}
</select>
<p>
My text
</p>
</body>
</html>
""".format(
        OPTIONS=html_list,
        )
print(html)
It also makes it quicker and easier to plugin a value like OPTIONS here. NOTE: you do need the blank line on line 2.


and you cant link the script as if its in the same directory. You have to make the path to from root. Most likely on a linux server your cgi would be in a cgi-bin directory, so even though the scripts are in the same directory, you still need to link it from the root directory. 

so lets say html is the root
/var/www/html

and your scripts live here
/var/www/html/cgi-bin

even though both CSS and your cgi script are in cgi-bin, you need to use 
<link type="text/css" rel="stylesheet" href="/cgi-bin/styles.css" />
instead
Recommended Tutorials:
Reply
#3
sorry css not working .....again..

#!/usr/bin/python3.5
import cgi

html = """Content-type: text/html\r\n\r\n
 
<html>
<head>
<link type="text/css" rel="stylesheet" href="/cgi-bin/cgi/style.css" />
</head>
<body>
<h1>
My text
</h1>
</body>
</html>
"""
print(html)
i cant figure out whats the problem
Reply
#4
Please sto using CGI in Python,it's dead after WSGI.

Use Flask
Eg.
pip install flask
Folder setup:
foo\
app.py
  templates\
  index.html
  static\
    css\
    style.css
app.py
from flask import Flask, render_template

app = Flask(__name__)
@app.route("/")
def bak_image():
    return render_template("index.html")

if __name__ == '__main__':
    app.run()
index.html
<html>
  <head>
    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />   
  </head>
  <title>
    Wsgi trough Flask
  </title>  
  <body>
    <h1>My name is zzzzzz</h1>
  </body>
</html>
style.css
*{
  padding: 0px;
  margin: 0px;
  font-size: 20px;
}
 
h1{
  color: blue;
}
Run python app.py.
In browser http://127.0.0.1:5000/
Reply
#5
(Nov-11-2016, 03:00 PM)snippsat Wrote: Please sto using CGI in Python,it's dead after WSGI.
that doesnt stop people from using it

if this is your path
/cgi-bin/cgi

then it should work. You might want to check the log files for a better description to why it might not be working.
Recommended Tutorials:
Reply
#6
What does "it doesn't work" mean? Do you get errors in your browser's development console? When you browse to the file in your browser, does it download?
Reply
#7
i use lampp server at ubuntu 15.10.
the css path is :
/cgi-bin/cgi/
and i write it on my code but not change anything

(Nov-12-2016, 06:41 AM)x64 Wrote: i use lampp server at ubuntu 15.10.
the css path is :
/cgi-bin/cgi/
and i write it on my code but not change anything
where i found the log file
Reply
#8
view the apache error log, and see what it says when you try to access that page from a browser
Recommended Tutorials:
Reply
#9
As you have wrote it ( "/style.css" ) the css file is located in /. That means the root directory of the whole system. If it's located at the same directory it should be "style.css" or "./style.css". Not sure for the second one.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I need litte help with linking to another website. darktitan 3 2,065 Nov-08-2019, 01:17 PM
Last Post: darktitan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020