Django

[Django] user CRUD(6) - 회원 가입(C)

기록하는_사람 2022. 12. 21. 18:58

회원 가입

📌 회원 가입

① index.html에 회원 가입 버튼 생성.

<h1>INDEX PAGE</h1>

{% if user.is_authenticated %}
    <a href="{% url 'profile' %}"><button>PROFILE</button></a>
    <a href="{% url 'logout' %}"><button>LOGOUT</button></a>
{% else %}
    <a href="{% url 'login' %}"><button>LOGIN</button></a>
    <a href="{% url 'signup' %}"><button>SIGNUP</button></a>
{% endif %}

 

② signup.html

<h1>SIGNUP PAGE</h1>

<form method="post">
    {% csrf_token %}
    <input name="uname" type="text" placeholder="USERNAME"><br><br>
    <input name="upass" type="password" placeholder="PASSWORD"><br><br>
    <button>SIGNUP</button>
    <a href="{% url 'index' %}"><button type="button">HOME</button></a>
</form>

 

③ acc/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('index/', views.index, name="index")   ,
    path('login/', views.userlogin, name="login"),
    path('logout/', views.userlogout, name="logout"),
    path('profile/', views.profile, name="profile"),
    path('delete/', views.delete, name="delete"),
    path('signup/', views.signup, name="signup")
]

 

④ acc/views.py

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from .models import User

# Create your views here.
...

def signup(request):
    if request.method == "POST":
        un = request.POST.get("uname")
        up = request.POST.get("upass")
        try: 
            User.objects.create_user(username=un, password=up)
            return redirect("login")
        except:
            pass
    return render(request, "acc/signup.html")