栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

django 前端传参(变量、数据库)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

django 前端传参(变量、数据库)

一、将变量传入后端函数 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////', 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调用
 
{% csrf_token %}

重点在于value={{ question_id }}

2) href/url 调用
 

传参就直接在url 后就可以了(但是不能传变量)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348385.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号