jm_p_op

form 사용법 본문

py/django

form 사용법

jm_p_op 2023. 4. 13. 22:47

form을 만들어서 

modelForm의html을 사용하지 않고 기존 html에서 id값 받고 사용해도 erro값과 .is_valid사용 가능

forms.py

class PasswordForm(UserModel):
    password2 = models.CharField(max_length=100)

class UserUpErro(forms.ModelForm):
    password2 = models.CharField()
    class Meta:
        model = PasswordForm
        fields=['username','email','password','password2']
        widgets = {"password": PasswordInput(), "password2": PasswordInput()}

 

signup.html

<form class="form-area" method="post" action="/signup/">
  {% csrf_token %}
  <!-- CSRF보호를 위한 태그,POST요청 시 안전하게 보호-->
  <div class="form-group mt-2 mb-2">
    <label for="username">이름</label>
    <input type="text" required class="form-control" id="username" name="username">
  </div>
  <div class="form-group mt-2 mb-2">
    <label for="email">이메일</label>
    <input type="email" required class="form-control" id="email" name="email">
  </div>
  <div class="form-group mt-2 mb-2">
    <label for="password">비밀번호</label>
    <input type="password" required class="form-control" id="password" name="password">
  </div>
  <div class="form-group mt-2 mb-2">
    <label for="password2">비밀번호 확인</label>
    <input type="password" required class="form-control" id="password2" name="password2">
  </div>
  <hr>
  <div style="float: right">
    <!--form태그의 method로 action에 데이터를 보내주는 역할-->
    <button type="submit" class="btn btn-primary">회원가입</button>
  </div>
</form>

view.py

form = UserUpErro(request.POST)
print(form.errors)

 

'py > django' 카테고리의 다른 글

django, gitignore- migrations/ _initial.py 삭제  (0) 2023.04.19
django-python-Model.save(), 저장도중 함수 실행  (0) 2023.04.17
django - ManytoManyField  (0) 2023.04.12
django-models.ForeignKey()  (0) 2023.04.11
django- form 형식  (0) 2023.04.10