django 프레임 워 크 가 실 현 된 사용자 등록,로그 인,종료 기능 예시

본 고의 실례 는 django 프레임 워 크 가 실 현 된 사용자 등록,로그 인,종료 기능 을 설명 한다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
1 사용자 등록:

from django.contrib import auth
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect
#     
@csrf_exempt
def register(request):
  errors = []
  account = None
  password = None
  password2 = None
  email = None
  CompareFlag = False
  if request.method == 'POST':
    if not request.POST.get('account'):
      errors.append('       ')
    else:
      account = request.POST.get('account')
    if not request.POST.get('password'):
      errors.append('      ')
    else:
      password = request.POST.get('password')
    if not request.POST.get('password2'):
      errors.append('        ')
    else:
      password2 = request.POST.get('password2')
    if not request.POST.get('email'):
      errors.append('      ')
    else:
      email = request.POST.get('email')
    if password is not None:
      if password == password2:
        CompareFlag = True
      else:
        errors.append('         ')
    if account is not None and password is not None and password2 is not None and email is not None and CompareFlag :
      user = User.objects.create_user(account,email,password)
      user.save()
      userlogin = auth.authenticate(username = account,password = password)
      auth.login(request,userlogin)
      return HttpResponseRedirect('/blog')
  return render(request,'blog/register.html', {'errors': errors})

2 사용자 로그 인:

@csrf_exempt
def my_login(request):
  errors =[]
  account = None
  password = None
  if request.method == "POST":
    if not request.POST.get('account'):
      errors.append('       ')
    else:
      account = request.POST.get('account')
    if not request.POST.get('password'):
      errors = request.POST.get('      ')
    else:
      password = request.POST.get('password')
    if account is not None and password is not None:
      user = auth.authenticate(username=account,password=password)
      if user is not None:
        if user.is_active:
          auth.login(request,user)
          return HttpResponseRedirect('/blog')
        else:
          errors.append('     ')
      else:
        errors.append('        ')
  return render(request,'blog/login.html', {'errors': errors})

3 사용자 종료:

def my_logout(request):
  auth.logout(request)
  return HttpResponseRedirect('/blog')

URL:

urlpatterns = [
  url(r'^$', views.index, name='index'),
  url(r'^p/(?P<article_id>[0-9]+)/$', views.detail,name='detail'),
  url(r'^register/$',views.register, name='register'),
  url(r'^login/$',views.my_login, name='my_login'),
  url(r'^logout/$',views.my_logout, name='my_logout'),
]

HTML 등록:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
{% if errors %}
    <li>
      {% for error in errors %}
       <p style="color: red;">
        {{error}}
       </p>
       {% endfor %}
    </li>
  {% endif %}
<table>
  <form action="" method="post">{% csrf_token %}
    <tr>
      <td>
        <label >   :</label>
      </td>
      <td>
        <input type = 'text' placeholder="     " name = 'account'>
      </td>
    </tr>
    <tr>
      <td>
        <label >  :</label>
      </td>
      <td>
       <input type = 'password' placeholder="    " name = 'password'>
      </td>
    </tr>
     <tr>
       <td>
        <label >    :</label>
       </td>
       <td>
         <input type = 'password' placeholder="      " name ='password2'>
       </td>
     </tr>
     <tr>
       <td>
         <label>  :</label>
       </td>
       <td>
         <input type="email" placeholder="    " name = 'email'>
       </td>
     </tr>
     <tr>
       <td>
          <input type = 'submit' placeholder="Login" value="  ">
       </td>
     </tr>
  </form>
</table>
</body>
</html>

로그 인 HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>  </title>
</head>
<body>
{% if errors %}
    <li>
      {% for error in errors %}
       <p style="color: red;">
        {{error}}
       </p>
       {% endfor %}
    </li>
  {% endif %}
<table>
  <form action="" method="post">{% csrf_token %}
    <tr>
      <td>
        <label >   :</label>
      </td>
      <td>
        <input type = 'text' placeholder="     " name = 'account'>
      </td>
    </tr>
    <tr>
      <td>
        <label >  :</label>
      </td>
      <td>
       <input type = 'password' placeholder="    " name = 'password'>
      </td>
    </tr>
     <tr>
       <td>
          <input type = 'submit' placeholder="Login" value="  ">
       </td>
     </tr>
  </form>
</table>
</body>
</html>
</body>
</html>

본 고 는 Django 프레임 워 크 를 바탕 으로 하 는 Python 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기