SMALL
버튼이벤트 구현하기.
구현할 내용: motion과 sound alarm setting 버튼을 누를 때 각각 값을 db에 저장.
값을 저장하기 위해 save버튼을 생성했음.
save버튼 클릭시 해당 page에 post로 전송되며, 이 때 check box들의 상태를 view에 전달하도록 구현.
html 일부:
<form action="" method="post">
<tr>
<th align="right">motion</th>
<th align="left">
<label class="switch">
{% if motion_alarm %}
<input type="checkbox" name="alarms" value="motion" checked>
{% else %}
<input type="checkbox" name="alarms" value="motion">
{% endif %}
<span class="slider round"></span>
</label>
</th>
<th align="right">sound</th>
<th align="left">
<label class="switch">
{% if sound_alarm %}
<input type="checkbox" name="alarms" value="sound" checked>
{% else %}
<input type="checkbox" name="alarms" value="sound">
{% endif %}
<span class="slider round"></span>
</label>
</th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
<th>
<button name="savebtn" class="button1" type="submit">save</button>
</th>
</tr>
</form>
views.py:
def index_mac(request, mac_addr):
#1. find mac_addr from userlist
user = User.objects.filter(Q(mac_address=mac_addr))
if request.method == 'GET':
count = user.count()
if count == 0:
print('create user')
new_user = User(mac_address=mac_addr)
new_user.save()
alarm_setting = AlarmSetting(user=new_user)
alarm_setting.save()
else:
print('This mac address is registered already', user)
elif request.method == 'POST':
user = User.objects.get(mac_address=mac_addr)
#2. get and save checkbox value to db
alarm = AlarmSetting.objects.get(user=user.id)
check_status = request.POST.getlist('alarms')
print('check_status=', check_status)
if 'motion' in check_status:
alarm.motion_alarm = True
else:
alarm.motion_alarm = False
if 'sound' in check_status:
alarm.sound_alarm = True
else:
alarm.sound_alarm = False
alarm.save()
#2. show user's alarm settings to web page
user = User.objects.get(mac_address=mac_addr)
alarm = AlarmSetting.objects.get(user=user.id)
context = {
'user_id': user.id,
'motion_alarm': alarm.motion_alarm,
'sound_alarm': alarm.sound_alarm,
}
return render(request, 'ipcamera_for_baby/video_feed.html', context)
save버튼을 누르면 db에 저장되고, 해당 페이지 로딩시 db에서 값을 불러와 checkbox에 표시하도록 구현 됨.
LIST
'개발Study > IP Camera' 카테고리의 다른 글
make IP camera using raspberry pi 4 (10) android web view (0) | 2022.02.21 |
---|---|
make IP camera using raspberry pi 4 (9) motion alarm setting (0) | 2022.02.20 |
make IP camera using raspberry pi 4 (7) model (0) | 2022.02.12 |
make IP camera using raspberry pi 4 (6) web ui with django (0) | 2022.02.09 |
make IP camera using raspberry pi 4 (5) django server (0) | 2022.01.28 |
댓글