我有一个 django 视图,其中包含带有表单和一些按钮的 html 模板。当按下其中一个按钮时,它会发送带有查询参数的 get 请求,并根据此参数再次渲染页面,但使用不同的 json 参数。但是我无法通过第二次返回来返回网页。
这是问题视图的重要代码。我也尝试过这个没有返回,但它也不起作用。我尝试使用 JsonResponce,但没有成功:
def card(request):
cur_rk = 0
if request.method == 'GET':
if request.GET.get('C', '') == '-1':
q1 = "SELECT MAX(RK) AS L_RK FROM B_DATA;"
with connection.cursor() as cursor:
cursor.execute(q1)
val = cursor.fetchall()
cur_rk = val[0][0]
return render(request, "card.html", get_db_data(cur_rk))
return render(request, "card.html", get_db_data(cur_rk))
这里是 html 代码,如果它可以帮助你:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create new card</title>
</head>
<body>
<div class = "enter_data">
<form action = "" method="POST">{% csrf_token %}
<div class = "base">
<h2>Базови данни</h2>
<input type="number" class="field" placeholder="Enter RK" value = {{RK}} name="RK">
<input type="text" class="field" placeholder="Enter RN" value = {{RN}} name="RN">
<input type="text" class="field" placeholder="Enter Marka" value = {{Marka}} name="Marka">
<input type="text" class="field" placeholder="Enter Model" value = {{Model}} name="Model">
<input type="number" class="field" placeholder="Enter G_PR" value = {{G_PR}} name="G_PR">
<input type="number" class="field" placeholder="Enter KM" value = {{KM}} name="KM">
<input type="text" class="field" placeholder="Enter Kupe" value = {{Kupe}} name="Kupe">
<input type="text" class="field" placeholder="Enter Rama" value = {{Rama}} name="Rama">
<input type="text" class="field" placeholder="Enter Dvigatel" value = {{Dvigatel}} name="Dvigatel">
<textarea class="field" placeholder="Enter Descr" name="Descr">{{Descr}}</textarea>
<textarea class="field" placeholder="Enter Problem" name="Problem">{{Problem}}</textarea>
<input type="date" class="field" placeholder="Enter R_DATA" value = {{R_DATA}} name="R_DATA">
</div>
<div class = "client">
<h2>Данни за клиента</h2>
<input type="text" class="field" placeholder="Enter ime" value = {{ime}} name="ime">
<input type="text" class="field" placeholder="Enter telefon" value = {{telefon}} name="telefon">
</div>
<button type = "submit" class = "btn">Add card</button>
</form>
</div>
<div class = "control_btns">
<button id="first" onclick="getData(0)"><<</button>
<button id="previous" onclick="getData({{RK}}-1)"><</button>
<button id="next" onclick="getData({{RK}}+1)">></button>
<button id="last" onclick="getData(-1)">>></button>
</div>
<script>
function getData(card) {
let url = 'http://127.0.0.1:8000/card/?C=' + card.toString();
fetch(url);
}
function greet() {
console.log('Hey there clicker!');
}
</script>
</body>
</html>
要管理多个返回语句,请确保视图逻辑结构良好。看来您想根据查询参数
C
以不同的方式呈现页面。在这种情况下,我建议使用单个渲染语句,因为上下文看起来相似。
def get_db_data(rk):
# This should return a dictionary of data to be passed to the template
return {
'cur_rk': rk,
# ...
}
def card(request):
cur_rk = 0
if request.method == 'GET':
if request.GET.get('C', '') == '-1':
q1 = "SELECT MAX(RK) AS L_RK FROM B_DATA;"
with connection.cursor() as cursor:
cursor.execute(q1)
val = cursor.fetchall()
cur_rk = val[0][0]
context = get_db_data(cur_rk)
return render(request, "card.html", context)