我有一个功能,可以按需显示车辆的实时位置。有两个功能,因此,单击按钮应在内部运行代码,并在同一视图/页面上显示结果。我不了解如何创建这两个功能,而无需重新加载页面或转到另一个页面。到目前为止,我不必在模板的函数中传递任何类型的参数。在Django中有可能吗?
这是我视图中的功能。py
@method_decorator([login_required, teacher_required], name='dispatch')
class QuizResultsView(DetailView):
model = Quiz
context_object_name = 'quiz'
template_name = 'classroom/teachers/quiz_results.html'
tripId = str()
def start_trip(self):
quiz = self.get_object()
lr_object = get_object_or_404(LR, lr_quiz=quiz.id)
# ----------Call api to fetch location code -----------#
我的api提取代码将返回一个我必须解析的json对象,并将其输入到Google Iframe中,并且填充iframe大约需要2-3秒。
我有很多“迷你功能”或在同一个功能中被称为的所有功能,就像:
def get_queryset (self):
return self.request.user.quizzes.all()
因此,有没有办法像这样创建更多的功能?另外,如何从模板中调用此类函数?还有其他方法可以创建视图,在该视图中我可以有多个按钮,每个按钮具有各自的功能,并且它不会重新加载页面或转到任何其他页面吗?
def start_trip (self):
quiz = self.get_object()
lr_object = get_object_or_404(LR, lr_quiz=quiz.id)
print("@271 lr_object.driver_name", lr_object.driver_name)
gmaps = googlemaps.Client(key=###)
driver_num = lr_object.driver_name
origin = lr_object.lr_consignee_address # origin from lR
dest = lr_object.lr_consignor_address # dest from LR
geocode_result_orig = gmaps.geocode(origin)
geocode_result_orig = gmaps.geocode(origin)
lat_orig = str(geocode_result_orig[0]["geometry"]["location"]["lat"])
lon_orig = str(geocode_result_orig[0]['geometry']['location']['lng'])
geocode_result_dest = gmaps.geocode(dest)
lat_dest = str(geocode_result_dest[0]["geometry"]["location"]["lat"])
lon_dest = str(geocode_result_dest[0]['geometry']['location']['lng'])
url = 'https://sct.intutrack.com/api/prod/trips/start'
payload = 'tel=' + driver_num + '&src=' + lat_orig + '%2C%20' + lon_orig + '&dest=' + lat_dest + '%2C%20' + lon_dest + '&srcname=Intugine' \
'%20Technologies&destname=Bangalore&client=XYZ&vendor=Rivigo&vehicle=' \
'MH01x123&invoice=123ZXYZ&eta_hrs=75'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
username = '###'
password = '###'
user_pass = (username, password)
response = requests.request('POST', url, headers=headers, data=payload, auth=user_pass)
response = response.json()
tripId = response["tripId"]
更新: 我想这只能用javascript完成,所以我猜我的问题变成了'如何将django变量传递给javascript函数(无需重新加载页面ofc。)?'