一、将变量传入后端函数
1、前端function
var url = "{% url 'apply_experiments:change_experiments' 'school_year' 'term' 'course' %}";
url = url.replace('school_year/term/course', row.school_year+'/'+row.term+'/'+row.course);
window.location.href=url;
2、路由
path('change-experiments/<school_year>/<term>/<course>/', change_experiments, name='change_experiments'),
3、视图views.py
def change_experiments(request, school_year=None, term=None, course=None):
context = {
'status': True,
}
this_logger.info('实验项目申请信息管理页面视图,接收到:学年:'+str(school_year)+' 学期:'+str(term)+' 课程:'+str(course))
return render(request, 'apply_experiments/change_experiments.html', context)
二、将context上下文的字典传回后端
1、后端获得参数示例
def question_content(request, question_id):
question = Question.objects.get(id=question_id)
question_2 = Question.objects.all().order_by('-views', 'created', 'questionTitle')[:10]
question.views = question.views + 1
question.save()
answer_list = AnswerModel.objects.filter(question_id=question_id)
questions = Question.objects.all()
context = {
"question": question,
"question_id": question_id,
"answer_list": answer_list,
"questions": questions,
"question_2": question_2,
}
return render(request, "question/content.html", context=context)
2、前端传回有两种
1) Ajax调用
<form class="form-horizontal" id="modalForm_Content" method="post" action="">{% csrf_token %}
<input type="text" class="form-control" id="form_name" name="name" placeholder="实验/文件夹名称">
<input type="text" class="form-control" id="form_question_id" name="question_id" value="{{ question_id }}" requiredplaceholder="" hidden>
<button type="submit" class="btn btn-default" onclick="submit_type=1">SAVE</button>
重点在于value={{ question_id }}
2) href/url 调用
<a href="{% url 'question:question_content' question_id=question.id %}" class="news-item">
传参就直接在url 后就可以了(但是不能传变量)