📢 공지사항
home

DetailView를 이용한 개인 페이지 구현

수강계획
2022/01/11
번호
24
복습여부
수강여부
수강일
2022/01/12
속성
views.py에 detail view 만들기
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render # Create your views here. from django.urls import reverse, reverse_lazy from django.views.generic import CreateView, DetailView from accountapp.models import HelloWorld def hello_world(request): if request.method == "POST": temp = request.POST.get('hello_world_input') new_hello_world = HelloWorld() new_hello_world.text = temp new_hello_world.save() hello_world_list = HelloWorld.objects.all() return HttpResponseRedirect(reverse('accountapp:hello_world')) else: hello_world_list = HelloWorld.objects.all() return render(request, 'accountapp/hello_world.html', context={'hello_world_list': hello_world_list}) class AccountCreateView(CreateView): model = User form_class = UserCreationForm success_url = reverse_lazy('accountapp:hello_world') template_name = 'accountapp/create.html' class AccountDetailView(DetailView): model = User context_object_name = 'target_user' template_name = 'accountapp/detail.html'
Python
복사
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> <h2 style="font-family: 'NanumSquareB'"> {{ target_user.username }} </h2> </div> </div> {% endblock %}
HTML
복사
target_user 왜 사용한다는거지 ,,?
from django.contrib.auth.views import LoginView, LogoutView from django.urls import path from accountapp.views import hello_world, AccountCreateView, AccountDetailView app_name = "accountapp" urlpatterns = [ path('hello_world/',hello_world, name='hello_world'), path('login/', LoginView.as_view(template_name='accountapp/login.html'), name='login'), path('logout/',LogoutView.as_view(), name='logout'), path('create/', AccountCreateView.as_view(), name='create'), path('detail/<int:pk>', AccountDetailView.as_view(), name='detail'), ]
Python
복사
urls.py에 detailview 라우팅하기
detail 같은 경우에는 특정 유저의 정보를 봐야함
그 계정의 primary key가 필요함
그래서 <int:pk> pk라는 이름의 int 정보를 받겠다는거
<div class="Jichuuu_header"> <div> <h1 class="Jichuuu_logo">Jichuuu</h1> </div> <div> <span>nav1</span> <span>nav2</span> <span>nav3</span> {% if not user.is_authenticated %} <a href="{% url 'accountapp:login' %}?next={{ request.path }}"> <span>login</span> </a> {% else %} <a href="{% url 'accountapp:detail' pk=user.pk %}"> <span>MyPage</span> </a> <a href="{% url 'accountapp:logout' %}?next={{ request.path }}"> <span>logout</span> </a> {% endif %} </div> </div>
HTML
복사
header.html - mypage 링크로 향하는거 만들기 pk에 해당하는 값을 넣어줘야함