首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在html模板DJANGO中更改变量

如何在html模板DJANGO中更改变量
EN

Stack Overflow用户
提问于 2020-12-02 16:12:24
回答 1查看 35关注 0票数 0

我在我的上下文字典中写了这个变量(重构)。

代码语言:javascript
复制
@login_required(login_url='/login')
def lessont(request, id):
if request.method == 'POST':
    form = ChapterForm(request.POST, request.FILES)
    if form.is_valid():
        new_chapter = Chapter.objects.create(lesson_id=id)
        new_chapter.name = form.cleaned_data['name']
        new_chapter.description = form.cleaned_data['description']
        new_chapter.document = form.cleaned_data['document']
        new_chapter.save()
        return redirect('/lessont/lesson/<int:id>')
get_lessons = Lesson.objects.get(id=id)
get_chapter = Chapter.objects.all().filter(lesson=id)
get_group = StudentsGroup.objects.all().filter(lessons=id)
form = ChapterForm()
refactor = False
refactor_id = 0
context = {
    'get_lesson': get_lessons,
    'get_chapter': get_chapter,
    'get_group': get_group,
    'form': form,
    'refactor': refactor,   <----- HERE
    'refactor_id': refactor_id,
}
template = 'core/lessont.html'
return render(request, template, context) <----- pass it here

然后,在我的HTML模板中,我可以访问"refactor“变量,并且我想通过单击按钮来更改它

代码语言:javascript
复制
<button type="submit" name="button">Add</button>

我想做这样的事情:

代码语言:javascript
复制
<button type="submit" name="button" onClick={set "refactor" to True}>Add</button>
EN

回答 1

Stack Overflow用户

发布于 2020-12-02 16:25:46

在本例中,refactor是一个dict的值,而不是一个变量。

要在django模板中管理它,您可以尝试其中之一:

  • 传递locals()而不是context如果要使用变量而不是dict
  • 如果要在模板中使用context作为dict,则必须使用{{context.refactor}}获取您正在使用的外部实用程序(如sw-django-utils )的值,请考虑使用dict
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65104343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档