hyeonga_code

파이선 웹구축_장고_19_계정 회원가입 페이지 본문

Python_Django

파이선 웹구축_장고_19_계정 회원가입 페이지

hyeonga 2023. 6. 11. 05:59
반응형

- 계정 회원가입

1. 메인페이지에 회원가입 버튼 생성 <templates> > <adm> > 'index.html'

'index.html'

=====

1
2
3
4
5
6
7
8
<h1><b>MAIN PAGE</b></h1>
<br><br>
 
{% if request.user.is_authenticated %}
 
{% else %}
    <a href="{% url 'signup' %}"><button>Signup</button></a>
{% endif %}
cs

 

2. 경로 설정 <adm> > 'urls.py'

'urls.py'

=====

1
2
3
4
5
6
from django.urls import path, include
from . import views
 
urlpatterns = [
    path('signup/', views.signup, name='signup'),
]
cs

 

3. 회원가입 페이지 작성 <templates> > <adm> > 'signup.html'

'signup.html'

=====

1
2
3
4
5
6
7
8
9
10
11
12
<h1><b>SIGNUP PAGE</b></h1>
<br><br>
 
<form method='POST' enctype='multipart'>
    {% csrf_token %}
    #_ 페이지의 폼을 암호화하는 작업입니다. 
#_ 다른 사이트에서 수정, 삭제등의 공격을 막기 위한 장고에서 제공
    <input type="text" name='uname' placeholder='Input Username'><br><br>
    <input type='password' name='upass' placeholder='Input Password'><br><br>
    <button>Signup</button>
    <a href="{% url 'index' %}"><button type='button'>Cancle</button></a>
</form>
cs

 

4. 함수 생성 <templates> > <adm> > 'views.py'

'views.py'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from .models import User
#_ 추가
 
def signup(request):
    if request.method=="POST":
        un = request.POST.get('uname')
        up = request.POST.get('upass')
        User.objects.create_user(username=un, password=up)
        #_ 암호가 평문으로 저장되어 로그인이 안되는 것이므로 이렇게 작성해야 함
        return redirect('login')
    return render(request, "adm/signup.html")
cs

 

반응형