-
Django 초간단 게시판 8. SignUp (회원가입)Python/초간단 장고 Django 2021. 5. 28. 15:08반응형
대부분의 장고 개발자들은 별도의 앱을 만들어서 유저를 관리한다.
accounts 앱을 만들자.
(venv) C:\pyProjects\djangoBBS>python manage.py startapp accounts
config/settings.py 에도 등록하고..
# config/settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'board.apps.BoardConfig', 'accounts.apps.AccountsConfig', ]
뷰도 만들고...
# accounts/views.py from django.contrib.auth.forms import UserCreationForm from django.urls import reverse_lazy from django.views.generic import CreateView class SignUp(CreateView): form_class = UserCreationForm success_url = reverse_lazy('login') template_name = 'accounts/signup.html'
'accounts/urls.py'도 만들고
# accounts/urls.py from django.urls import path from .views import SignUp urlpatterns = [ path('signup/', SignUp.as_view(), name='signup'), ]
'config/urls.py'도 수정하고...
# config/urls.py from django.contrib import admin from django.urls import include, path urlpatterns = [ path('', include('board.urls')), path('admin/', admin.site.urls), path('accounts/', include('django.contrib.auth.urls')), path('accounts/', include('accounts.urls'), name='accounts'), path('board/', include('board.urls'), name='board'), ]
accounts/templates/accounts/signup.html 만들면...
<!--accounts/templates/accounts/signup.html--> <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>Sign up</h2> <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Sign Up</button> </form> </body> </html>
다소 verbose 하지만... 나쁘지 않은 사인 업 화면을 볼 수 있다.
게시판에 변화를 적용하자.
<!--board/templates/board/article_list.html--> <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% if object_list %} <ul> {% for each in object_list %} <li> <a href="{% url 'detail' each.id%}"> {{each.id}} / {{each.author}} / {{ each.title }} / {{each.created_at}} </a> </li> {% endfor %} </ul> {% else %} <p>목록이 비었습니다.</p> {% endif %} {% if is_paginated %} <ul> {% if page_obj.has_previous %} <li><a href="?page={{ page_obj.previous_page_number }}">«</a></li> {% else %} <li><span>«</span></li> {% endif %} {% for i in paginator.page_range %} {% if page_obj.number == i %} <li><span>{{ i }} <span>(current)</span></span></li> {% else %} <li><a href="?page={{ i }}">{{ i }}</a></li> {% endif %} {% endfor %} {% if page_obj.has_next %} <li><a href="?page={{ page_obj.next_page_number }}">»</a></li> {% else %} <li><span>»</span></li> {% endif %} </ul> {% endif %} <div><a href="{% url 'create' %}">글 등록</a></div> {% if user.is_authenticated %} <div><p>Hi {{ user.username }}!</p></div> <div><a href="{% url 'logout' %}">Log Out</a></div> {% else %} <div><p>You are not logged in</p></div> <div><a href="{% url 'login' %}">Log In</a> or <a href="{% url 'signup' %}">Sign Up</a></div> {% endif %} </body> </html>
참고 : https://learndjango.com/tutorials/django-signup-tutorial
반응형