CSS not linking with CGI script - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: CSS not linking with CGI script (/thread-878.html) |
CSS not linking with CGI script - x64 - Nov-11-2016 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 RE: Can not work css file in python cgi - metulburr - Nov-11-2016 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 RE: CSS not linking with CGI script - x64 - Nov-11-2016 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 RE: CSS not linking with CGI script - snippsat - Nov-11-2016 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.cssapp.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/ RE: CSS not linking with CGI script - metulburr - Nov-11-2016 (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. RE: CSS not linking with CGI script - nilamo - Nov-11-2016 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? RE: CSS not linking with CGI script - x64 - Nov-12-2016 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. RE: CSS not linking with CGI script - metulburr - Nov-12-2016 view the apache error log, and see what it says when you try to access that page from a browser RE: CSS not linking with CGI script - wavic - Nov-14-2016 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. |