mittlerweile habe ich mit etwas in Bottle einarbeiten können (nach meinen ersten Versuchen: viewtopic.php?f=1&t=46152).
Jetzt scheitere ich etwas an der dynamischen Inhaltsaktualisierung (via XMLHttpRequest - aka Ajax).
Habt ihr vielleicht ein Minimalbeispiel für mich (ich habe das bei keinem Bottle-Tutorial gefunden)? Bzw. dürft Ihr Euch gern mein nicht funktionierendes Beispiel anschauen:
Meine Testwebseite (ignoriert mal die MySQL-Datenbankabfrage sowie Kommentare):
Code: Alles auswählen
@app.route('/hello')
def hello():
# if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
# return 'This is an AJAX request'
# else:
# return 'This is a normal request'
try:
dbcon = mysql.connector.connect(
host = "127.0.0.1",
user = "root",
passwd = "",
database = "test"
)
except mysql.connector.Error as e:
strErr = "Error:", e.errno # error number
strErr += ": ", e.msg # error message
#return strErr
return template('error', result=strErr, template_lookup=['c:/Daten/02_Projekte/Prj_008_Firma/50_Projekte/testBottle/static'] ) #absoluten Pfad angeben, ganz hinten darf kein Slash stehen
cursor = dbcon.cursor()
# Datensätze auslesen
cursor.execute("SELECT * from anlagen")
result = cursor.fetchall()
cursor.close() # immer cursor schließen nach jedem Befehl
# for data in result:
# #strTest = "name: " + str(data[3]) + "; descr1: " + str(data[4])
# response += "name: " + str(data[3]) + "; descr1: " + str(data[4]) + "<br />"
# #response += "%(strTest)s<br />"
dbcon.close()
return template('index', result=result, template_lookup=['c:/Daten/02_Projekte/Prj_008_Firma/50_Projekte/testBottle/static'] ) #absoluten Pfad angeben, ganz hinten darf kein Slash stehen
Code: Alles auswählen
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link type="text/css" href="/static/mystyle.css" rel="stylesheet">
<script src="/static/context.js"></script>
<script src="/static/httpreq.js"></script>
</head>
<body oncontextmenu="return contextmenu()">
<table id="contexttable" class="system"></table>
<div id="show" class="system" style="padding: 1px;"></div>
<div id="testcm" hidden>
<h2>eigenes Kontextmenü</h2>
<ul>
<li><button id="hide">verstecken</button></li>
<li><button id="show">Anzeigen</button>
<ul>
<li><button id="showUrl">URL anzeigen</button></li>
<li><button id="showSrc">SRC anzeigen</button></li>
</ul></li>
<li><button id="move">1x normales Kontextmenü</button></li>
</ul>
</div>
<header>
header
</header>
<nav>
nav
<form action="">
<select name="customers" onchange="showCustomer(this.value)">
<option value="">Select a customer:</option>
<option value="ALFKI">Alfreds Futterkiste</option>
<option value="NORTS ">North/South</option>
<option value="WOLZA">Wolski Zajazd</option>
</select>
</form>
</nav>
<aside id="news">
Überraschung
</aside>
<article>
<ul>
% for item in result:
<li>{{item}}</li>
% end
</ul>
<div id="txtHint">Anzeige</div>
</article>
<aside>
bereich
</aside>
<footer>
footer
</footer>
</body>
</html>
Code: Alles auswählen
function showCustomer(str) {
var xhttp;
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/static/gettest.py?q="+str, true);
xhttp.send();
Viele Grüße,
dd0815