코딩공부/Python Django

django view에서 넘겨준 값 javascript로 받아주기

integerJI 2020. 7. 26. 22:13

 

프로필 수정을 하면서

 

디자인 수정을 하기 위해 form을 수정하였습니다.

 

하지만 html 하드코딩으로 디자인을 바꿔주니

 

사용자 정보를 못가져오네요

 

해당 html 입니다.

 

        <form method="POST" class="post-form" enctype="multipart/form-data" action="">
          {%csrf_token%}
          <div class="hr-sect">Profile_update</div>
          <br>
          <div class="form-group">
            <label for="formGroupExampleInput">이메일</label>
            <input type="email" name="user-username" class="form-control" maxlength="50" id="id_user-username" disabled>
          </div>
          <div class="form-group">
            <label for="formGroupExampleInput">비밀번호</label>
            <input type="password" name="user-password1" class="form-control" required id="id_user-password1">
          </div>
          <div class="form-group">
            <label for="formGroupExampleInput">비밀번호 확인</label>
            <input type="password" name="user-password2" class="form-control" required id="id_user-password2">
          </div>
          <div class="form-group">
            <label for="formGroupExampleInput">이름</label>
            <input type="text" name="profile-nick" class="form-control" autofocus required id="id_profile-nick">
          </div>
          <div class="row">
            <div class="col-md-4 mb-3">
              <label for="country">년도</label>
              <select class="custom-select d-block w-100" name="year" id="year" title="년도" required>
              </select>
            </div>
            <div class="col-md-4 mb-3">
              <label for="state">월</label>
              <select class="custom-select d-block w-100" name="month" id="month" title="월" required>
              </select>
            </div>
            <div class="col-md-4 mb-3">
              <label for="zip">일</label>
              <select class="custom-select d-block w-100" name="day" id="day" title="일" required>
              </select>
            </div>
          </div>
          <br>
          <button class="btn btn-lg btn-info btn-block" type="submit">Update Profile</button>
        </form>

 

input text칸에 기존 profile의 값을 넣어주겠습니다.

 

    def get(self, request):
        user = get_object_or_404(User, pk=request.user.pk) 
        conn_user = request.user
        conn_profile = Profile.objects.get(user=conn_user)

        if hasattr(user, 'profile'):  
            profile = user.profile
            profile_form = ProfileUpdateForm(initial={
                'nick': profile.nick,
                'birth_date' : profile.birth_date,
            })
        else:
            profile_form = ProfileUpdateForm()
            
        context = {
            'profile_form': profile_form,
            'profile': profile,
            'id' : conn_user.username,
            'nick' : conn_profile.nick,
        }

        return render(request, 'profile_update.html', context=context)

 

필요한건 user의 id와 이름, 생년 월일입니다.

 

get type으로 호출이 될때에도 값을 id값을 넘겨줍니다.

 

$('#id_user-username').val('{{id}}').text;
$('#id_profile-nick').val('{{nick}}').text;

 

이제 html script 단에서 id_user-username과 id_profile-nick라는 input type='text'의 id에 

 

val값을 view에서 넘겨준 {{id}}, {{nick}}로 받아 넣어줍니다.

 

 

정상적으로 잘 나오네요