Django-mysite만들기2 회원가입 기능 추가
20 Jun 2019 | django python ormdjango mini project
cafe24신입사원 교육과정 - django 수업 내용 정리
[1] application user 추가
Terminal -> python manage.py startapp user
[2] settings.py app 추가
INSTALLED_APPS = [
...
'user',
...
]
[3] Model 정의
user/models.py
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField(max_length=20)
email = models.CharField(max_length=200)
password = models.CharField(max_length=32)
gender = models.CharField(max_length=10)
joindate = models.DateTimeField(auto_now=True)
def __str__(self):
return f'User({self.name}, {self.email}, {self.password}. {self.gender}, {self.joindate}'
admi.py
from django.contrib import admin
# Register your models here.
from user.models import User
admin.site.register(User)
[4] migrate
python manage.py makemigrations
python manage.py migrate
DBeaver 확인
[5] 회원가입 기능 추가하기
urls.py 매핑 추가
path('user/joinform', user_views.joinform),
path('user/join', user_views.join),
path variable을 받을 때는
urls.py
path('user/join/<int:id>/<str:user_id>', helloworld_views.hello2),
views.py
def hello2(request, id=0, user_id=''): return HttpResponse(f'id:{id}, user_id:{user_id}')
이런식으로 받으면 된다.
views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from user.models import User
def joinform(request):
return render(request, 'user/joinform.html')
def joinsuccess(request):
return render(request, 'user/joinsuccess.html')
def join(request):
user = User()
user.name = request.POST['name']
user.email = request.POST['email']
user.password = request.POST['password']
user.gender = request.POST['gender']
user.save()
return HttpResponseRedirect('user/joinsuccess')
templates/base.html
templates/user/joinform.html
templates/user/joinsuccess.html
[6] Test
성공!
DB 확인