#Проблема: данные в list.html обновляются только после двукратного
#нажатия кнопи submit в форме
## в base.html есть inclusion tag {%user_settings%}
#он показыает форму с checkbox
#list.html - шаблон для view.course_list, который extends base.html
#в этом вью фильтруется в зависимости от данных из {%user_settings%}
### templatetags.py
# тут мы устанавливаем BooleanFields
# например show_online_only
@register.inclusion_tag('csun_ninja/user_settings.html', takes_context=True)
def user_settings(context):
request = context['request']
if request.user.is_authenticated():
form_instance = UserSettings.objects.get(student=request.user)
if request.method == "POST":
form = PreferenceForm(instance=form_instance,
data=request.POST)
if form.is_valid():
form.save()
else:
form = PreferenceForm(instance=form_instance)
return ({'form': form, 'request':request})
###### views.py
def course_list(request):
sections_list = Section.objects.all().order_by('course')
current_settings = UserSettings.objects.get(student=request.user)
# тут пропущено непринципиальное
if current_settings.show_online_only == True:
sections_list=sections_list.filter(section_schedule__section_room='ON LINE')
return render(request,
'csun_ninja/list.html',
{'sections':sections,
'section_schedules':section_schedules,
'page':page})
I9Cf0YDQvtCx0LvQtdC80LA6INC00LDQvdC90YvQtSDQsiBsaXN0Lmh0bWwg0L7QsdC90L7QstC70Y/RjtGC0YHRjyDRgtC+0LvRjNC60L4g0L/QvtGB0LvQtSDQtNCy0YPQutGA0LDRgtC90L7Qs9C+CiPQvdCw0LbQsNGC0LjRjyDQutC90L7Qv9C4IHN1Ym1pdCDQsiDRhNC+0YDQvNC1CgojIyDQsiBiYXNlLmh0bWwg0LXRgdGC0YwgaW5jbHVzaW9uIHRhZyB7JXVzZXJfc2V0dGluZ3MlfQoj0L7QvSDQv9C+0LrQsNC30YvQsNC10YIg0YTQvtGA0LzRgyDRgSBjaGVja2JveAoKI2xpc3QuaHRtbCAtINGI0LDQsdC70L7QvSDQtNC70Y8gdmlldy5jb3Vyc2VfbGlzdCwg0LrQvtGC0L7RgNGL0LkgZXh0ZW5kcyBiYXNlLmh0bWwKI9CyINGN0YLQvtC8INCy0YzRjiDRhNC40LvRjNGC0YDRg9C10YLRgdGPINCyINC30LDQstC40YHQu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