Skip to content

Latest commit

ย 

History

History
104 lines (89 loc) ยท 4.72 KB

File metadata and controls

104 lines (89 loc) ยท 4.72 KB

My page

์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€ ๋ชฉ๋ก์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋งˆ์ดํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. users app ์ƒ์„ฑ

$ python manage.py startapp users

๊ทธ๋ƒฅ main ์•ฑ์—์„œ ํ•ด๋„ ๊ดœ์ฐฎ์ง€๋งŒ, User์— ๋Œ€ํ•œ ์„œ๋น„์Šค๊ฐ€ ์ปค์ง€๋Š” ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด์„œ ๋‚˜๋ˆ„์–ด ๊ด€๋ฆฌ๋ฅผ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๊ธฐ ๋•Œ๋ฌธ์— ์•ฑ์„ ๋งŒ๋“ค๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ƒˆ๋กœ์šด ์•ฑ์„ ์ƒ์„ฑํ•˜์˜€์œผ๋‹ˆ basic/settings.py ์˜ INSTALLED_APPS์— ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

INSTALLED_APPS = [
    ...
    'users',
]

2. mypage.html ํŒŒ์ผ ์ƒ์„ฑ

users/templates/users/mypage.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

3. Profile ๋ชจ๋ธ ์ƒ์„ฑ

from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

django-allauth์—์„œ ์ œ๊ณตํ•˜๋Š” User ๋ชจ๋ธ์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” User ๋ชจ๋ธ๊ณผ 1:1๋กœ ๋Œ€์‘๋˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ์ฒ˜๋Ÿผ ๊ธ€ ๋ชฉ๋ก๋งŒ ๋ณด์—ฌ์ฃผ๊ธฐ์—๋Š” ์ถ”๊ฐ€ํ•ด์•ผํ•˜๋Š” ์†์„ฑ์€ ์—†์ง€๋งŒ ์•ž์œผ๋กœ ์ถ”๊ฐ€ํ•  ์†์„ฑ์ด ์ƒ๊ธธ ๊ฒƒ์ด๋ฏ€๋กœ user์•ฑ์„ ์ƒ์„ฑํ–ˆ์„ ๋•Œ Profile ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

1:1 ๋Œ€์‘์ด๋‹ˆ๊นŒ OneToOneField๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ๊ณ , User๊ฐ€ ์‚ญ์ œ๋˜๋ฉด ํ•ด๋‹นํ•˜๋Š” ํ”„๋กœํ•„๋„ ํ•จ๊ป˜ ์‚ญ์ œ๋˜๋„๋ก on_delete=models.CASCADE๋ฅผ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜์˜€์œผ๋‹ˆ migrateํ•ด์ค๋‹ˆ๋‹ค.

$ python manage.py makemigrations
$ python manage.py migrate

4. users/views.py

from django.shortcuts import render
from main.models import Blog

def mypage(request):
    user = request.user
    blogs = Blog.objects.filter(writer=user) # ๋กœ๊ทธ์ธํ•œ ์œ ์ €์ด๋ฆ„๊ณผ ๊ธ€ ์ž‘์„ฑ์ž ์ด๋ฆ„์ด ๋™์ผํ•œ ๊ธ€ ํ•„ํ„ฐ๋ง
    return render(request, 'users/mypage.html', {'blogs':blogs})

main์•ฑ์˜ Blog๋ชจ๋ธ์„ importํ•ด์ฃผ๊ณ , mypageํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. user๋Š” ํ˜„์žฌ ์ž‘์—…์ค‘์ธ ์œ ์ €์ด๊ณ , ๊ฒŒ์‹œ๊ธ€์€ Blog๋ชจ๋ธ์˜ ๊ฐ์ฒด๋“ค ์ค‘์— ์ž‘์„ฑ์ž๊ฐ€ ํ˜„์žฌ ์œ ์ €์— ๋Œ€ํ•œํ•˜๋Š” ๊ธ€์„ ํ•„ํ„ฐ๋ง ํ•˜์—ฌ ๋‹ด์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  mypage.html๋กœ renderํ•ด์ฃผ๋ฉฐ, ์ด๋•Œ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์œผ๋กœ blogs์— ํ•„ํ„ฐ๋งํ•œ ๊ธ€์„ ๋‹ด์•„์„œ ๋ณด๋‚ด์ฃผ์–ด์•ผ mypage.html์—์„œ blogs๋ฅผ ์ธ์‹ํ•˜์—ฌ ๊ฒŒ์‹œ๊ธ€์„ ๋„์›Œ์ค„ ์ˆ˜ ์žˆ๊ฒ ์ฃ .

5. url ์—ฐ๊ฒฐ

users/urls.py

from django.urls import path
from . import views

app_name = "users"
urlpatterns = [
    path('mypage/', views.mypage, name="mypage"),
]

users์•ฑ๋„ ์•ฑ๋งˆ๋‹ค url์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด urls.pyํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  url์„ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค. app_name๋„ ๊นŒ๋จน์ง€ ๋ง๊ณ  ์„ค์ •ํ•ด์ค์‹œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  basic/urls.py์— users ์•ฑ์˜ urls.py๋ฅผ includeํ•ด์ฃผ์–ด์•ผ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ .

urlpatterns = [
    ...
    path('users/', include('users.urls')),
]

6. mypage.html

users/templates/users/mypage.html

{% extends 'base.html' %}
{% load static %}

{% block content %}
<div class="container">
    <h1>{{user.username}}</h1>
    <div>
        {% for blog in blogs %}
            <a href="{%url 'main:detail' blog.id%}">{{blog.title}}</a><hr>
        {%endfor%}
    </div>
</div>
{% endblock %}

์ด์ œ template์„ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํ•ด์™”๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ base.html์„ ์ƒ์†๋ฐ›๊ณ  static๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ํ•จ๊ป˜ ์ ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ๋ถˆ๋Ÿฌ์™€์ค๋‹ˆ๋‹ค. ๋จผ์ € ํ˜„์žฌ ๋กœ๊ทธ์ธํ•œ ์œ ์ €์˜ ์ด๋ฆ„์„ ๋„์›Œ์ฃผ๊ณ  ์œ„ํ•ด {{user.username}}์„ ์ ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์•„๊นŒ mypageํ•จ์ˆ˜์—์„œ ํ•„ํ„ฐ๋งํ•˜์—ฌ ๊ธ€์„ ๋‹ด์€ blogs๊ฐ€ ์žˆ์—ˆ์ฃ . ์ด ๊ธ€๋“ค์„ for๋ฌธ์„ ์ด์šฉํ•ด ํ•˜๋‚˜์”ฉ blog๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด์•„์„œ ๋„์›Œ์ค๋‹ˆ๋‹ค. {{blog.title}}์„ ์ด์šฉํ•ด์„œ ๊ฒŒ์‹œ๊ธ€์˜ ์ œ๋ชฉ๋งŒ ๋ณด์—ฌ์ฃผ๊ณ , ํด๋ฆญํ•˜๋ฉด main์•ฑ์—์„œ ์ž‘์„ฑํ•ด์ฃผ์—ˆ๋˜ detail๋กœ ์ด๋™ํ•˜๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. main์•ฑ์˜ detailํ•จ์ˆ˜๋Š” id๊ฐ’์„ ์ธ์ž๋กœ ๋ฐ›์•„ id๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๊ธ€์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด์—ˆ์ฃ . ๊ทธ๋ž˜์„œ ๊ฒŒ์‹œ๊ธ€์˜ id๊ฐ’๋„ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.

7. mypage ๋ฒ„ํŠผ ์ถ”๊ฐ€ํ•˜๊ธฐ

๋งˆ์ง€๋ง‰์œผ๋กœ basic/templates/shared/_navbar.html์— mypage๋กœ ์ด๋™ํ•˜๋Š” ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค„๊ฒŒ์š”.

<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
  {% if user.is_authenticated %}
  <li><a class="dropdown-item" href="{%url 'users:mypage' %}">Mypage</a></li>
  <li><a class="dropdown-item" href="{%url 'account_logout' %}">Logout</a></li>
  {% else %}
  <li><a class="dropdown-item" href="{% url 'account_login' %}">Login</a></li>
  <li><a class="dropdown-item" href="{%url 'account_signup' %}">Signup</a></li>
  {% endif %}
</ul>

๋กœ๊ทธ์ธํ–ˆ์„ ๊ฒฝ์šฐ์— ๋งˆ์ดํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋Š” ๋งํฌ๊ฐ€ ์žˆ์–ด์•ผํ•˜๋ฏ€๋กœ {% if user.is_authenticated %}์— ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.