简体   繁体   中英

How to change variable in html template DJANGO

I wrote this variable(refactor) in my context dict.

@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

Then in my HTML template, I have an access to "refactor" variable and I want to change it by clicking the button

<button type="submit" name="button">Add</button>

I want to make something like:

<button type="submit" name="button" onClick={set "refactor" to True}>Add</button>

In this case refactor is the value of a dict and not a variable.

For manage it in the django template you can try one of those:

  • pass locals() instead of context if you want to use the variable and not the dict
  • if you want to use context as a dict, in the template you must use {{context.refactor}} for take the value ( doc )
  • If you are using external utils like sw-django-utils , consider to use get_key_from_dict

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM