# pragmatic/profileapp/models.py
from django.contrib.auth.models import User
from django.db import models
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
image = models.ImageField(upload_to='profile/', null=True)
nickname = models.CharField(max_length=20, unique=True, null=True)
message = models.CharField(max_length=100, null=True)
# 터미널 명령 실행
python manage.py makemigrations
python manage.py migrate

# pragmatic/profileapp/views.py
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from profileapp.forms import ProfileCreationForm
from profileapp.models import Profile
# Create your views here.
class ProfileCreateView(CreateView):
model = Profile # 모델은 우리가 만든 Profile 사용
context_object_name = 'target_profile'
form_class = ProfileCreationForm # 우리가 만든 ModelForm
success_url = reverse_lazy('accountapp:hello_world')
template_name = 'profileapp/create.html'
profileapp:create 로 변경<!--pragmatic/profileapp/templates/profileapp/create.html-->
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div style="text-align : center; max-width: 500px; margin: 4rem auto;">>
<div class="mb-4">
<h4>Profile Create</h4>
</div>
<!-- acoountapp:create -> profileapp:create -->
<form action="{% url 'profileapp:create' %}" method="post"> <!--url 일원화, post 방식 으로 전송 -->
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-dark rounded-pill col-6 mt-3">
</form>
</div>
{% endblock %}
# pragmatic/profileapp/urls.py
from django.urls import path
from profileapp.views import ProfileCreateView
app_name = 'profileapp'
urlpatterns = [
path('create/', ProfileCreateView.as_view(), name='create')
]
http://127.0.0.1:8000/profiles/create/ 로 접속
<!--pragmatic/accountapp/templates/accountapp/detail.html-->
{% extends 'base.html'%}
{% block content %}
<div>
<div style="text-align: center; max-width: 500px; margin: 4rem auto;">
<p>
{{ target_user.date_joined }}
</p>
<!-- 추가 -->
{% if target_user.profile %} <!--target_user 의 profile 이 존재한다면 -->
<h2 style="font-family: 'NanumSquareB'">
{{ target_user.profile.nickname }} <!--target_user 의 profile 의 nickname 보여주도록-->
</h2>
{% else %} <!-- 아닌경우 create profile 로 향하는 url 로 -->
<a href="{% url 'profileapp:create' %}"> <!--프로필을 만들수 있는 페이지로-->
<h2 style="font-family: 'NanumSquareB'">
Create Profile
</h2>
</a>
{% endif %}
<!-- 추가 끝 -->
{% if target_user == user %}
<a href="{% url 'accountapp:update' pk=user.pk%}">
<p>
Change Info
</p>
</a>
<a href="{% url 'accountapp:delete' pk=user.pk%}">
<p>
Quit
</p>
</a>
{% endif %}
</div>
</div>
{% endblock %}



<!--pragmatic/profileapp/templates/prolfileapp/create.html-->
<form action="{% url 'profileapp:create' %}" method="post" enctype="multipart/form-data">
enctype="multipart/form-data" 를 명시해야 한다.[NOT NULL constrain failed profileapp_profile user_id]user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile') fields = ['image','nickname','message'].save(commit=FALSE) : 실제 DB 에 저장하지는 않고 임시 데이터가 저장되는 것.from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from profileapp.forms import ProfileCreationForm
from profileapp.models import Profile
# Create your views here.
class ProfileCreateView(CreateView):
model = Profile # 모델은 우리가 만든 Profile 사용
context_object_name = 'target_profile'
form_class = ProfileCreationForm
success_url = reverse_lazy('accountapp:hello_world')
template_name = 'profileapp/create.html'
# 추가한 부분
def form_valid(self, form):
temp_profile = form.save(commit=False)
temp_profile.user = self.request.user
temp_profile.save()
return super().form_valid(form)
새로고침을 하고 파일을 선택 후 제출을 누르면

마이페이지로 이동한다면

