到目前为止,我正在尝试创建具有相关下拉菜单的表单,我已经能够添加所需的触发器,但是我只是注意到响应消息未呈现模板标签 我尝试添加模板标签的其他HTML文件也被忽略了 我不知道是什么原因导致这种错误
查看了其他一些帖子后,这些帖子说如果我要使用{%load XYZ%}。我应该在所有HTML页面中都使用这个load标签,但这不能解决我的问题
我的views.py:
from django.shortcuts import render,redirect
from django.views.generic.base import TemplateView
from status.forms import StatusForm
from status.models import FlightStatus
from django.views.generic import ListView, CreateView, UpdateView
from postlog.models import Flight
from django.urls import reverse_lazy
# Create your views here.
class StatusPageView(CreateView):
template_name = "status/home.html"
model = FlightStatus
form_class = StatusForm
success_url = reverse_lazy('status')
def load_park_bay(request):
flightid = request.GET.get('flight1')
parks = Flight.objects.filter(fl_no='flightid').order_by('park_bay')
context = {'parks': parks}
return render(request, 'status/park_bay_dropdown_list.html', context)
我的urls.py:
from django.urls import path,include
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('', views.StatusPageView.as_view(), name='status'),
path('ajax/load-park/', views.load_park_bay, name='ajax_load_park'),
]
显示表单的我的主页home.html:
{% extends 'base_template1.html' %}
{% load static from staticfiles %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
{% block title %} Status {% endblock %}
{% block content %}
<div class="modal fade" id="Add_Modal" tabindex="-1" role="dialog" aria-labelledby="Add_ModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="AddModalLabel">Flight Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" id="statusForm" data-parks-url="{% url 'ajax_load_park' %}" novalidate>
{% csrf_token %}
{{ form|crispy }}
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Add Details</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script>
$("#id_fl_no").change(function () {
var url = $("#statusForm").attr("data-parks-url");
var flightId = $(this).val();
$.ajax({
url: url,
data: {
'flight1': flightId
},
success: function (data) {
$("#id_park_bay").html(data);
}
});
});
</script>
{%endblock%}
html响应用作对ajax请求park_bay_dropdown.html的响应:
{% load static from staticfiles %}
{% load crispy_forms_tags %}
<option value="">---------</option>
{% for park in parks %}
<option value="{{ park.fl_no }}">{{ park.park_bay }}</option>
{% endfor %}
已收到回复:link 即使当我尝试将表单实例作为显示完整表单的上下文发送时,也没有显示任何模板标签
答案 0 :(得分:0)
由于您将来自ajax的'flight1'
的值存储在变量flightid
中。因此,您需要在查询中编写不带''的flightid。并且还要保持实践,将来自Ajax的id
转换为int
,以避免在通过ORM查询时出现任何错误。所以您的看法就是这样。
def load_park_bay(request):
flightid = int(request.GET.get('flight1'))
parks = Flight.objects.filter(fl_no=flightid).order_by('park_bay')
context = {'parks': parks}
return render(request, 'status/park_bay_dropdown_list.html', context)