Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6주차 Assignment - 김용욱 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file added kimyongwook/db.sqlite3
Binary file not shown.
Empty file added kimyongwook/likeapp/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file added kimyongwook/likeapp/__pycache__/apps.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions kimyongwook/likeapp/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions kimyongwook/likeapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class LikeappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'likeapp'
7 changes: 7 additions & 0 deletions kimyongwook/likeapp/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django import forms
from .models import Blog

class BlogForm(forms.ModelForm):
class Meta:
model = Blog
fields = ['title', 'body']
23 changes: 23 additions & 0 deletions kimyongwook/likeapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.6 on 2023-10-27 06:46

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Blog',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('pub_date', models.DateTimeField(verbose_name='data published')),
('body', models.TextField()),
],
),
]
26 changes: 26 additions & 0 deletions kimyongwook/likeapp/migrations/0002_blog_like_user_blog_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.7 on 2023-11-03 09:55

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('likeapp', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='blog',
name='like_user',
field=models.ManyToManyField(blank=True, related_name='like_user', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='blog',
name='user',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='blog', to=settings.AUTH_USER_MODEL),
),
]
20 changes: 20 additions & 0 deletions kimyongwook/likeapp/migrations/0003_blog_bookmark_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.3 on 2023-11-04 15:21

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('likeapp', '0002_blog_like_user_blog_user'),
]

operations = [
migrations.AddField(
model_name='blog',
name='bookmark_user',
field=models.ManyToManyField(blank=True, related_name='bookmark_user', to=settings.AUTH_USER_MODEL),
),
]
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 14 additions & 0 deletions kimyongwook/likeapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import models
from django.conf import settings
from django.contrib.auth import get_user_model

# Create your models here.
class Blog(models.Model):
title = models.CharField(max_length=100)
pub_date = models.DateTimeField('data published')
body = models.TextField()
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='blog', null=True)
like_user = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='like_user', blank=True)
bookmark_user = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='bookmark_user', blank=True)
def __str__(self):
return self.title
17 changes: 17 additions & 0 deletions kimyongwook/likeapp/templates/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'index.html' %}

{% block title %}
<title>Create</title>
{% endblock %}

{% block content %}
<h1>Create Page</h1>
<hr>
<form method="POST">
{% csrf_token %}
<table>
{{form.as_table}}
</table>
<button type = "submit">작성</button>
</form>
{% endblock %}
50 changes: 50 additions & 0 deletions kimyongwook/likeapp/templates/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{% extends 'index.html' %}

{% block title %}
<title>Detail</title>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css"/>
</head>
{% endblock %}

{% block content %}
<h1>Detail Page</h1>
<hr>
<h3>{{ blog.title }}</h3>
<p>{{ blog.pub_date }}</p>
<p>{{ blog.body }}</p>
<br>

<div>
<form action="{% url 'like' blog.id %}" method='POST'>
{% csrf_token %}
<button style="border : none">
{% if user in blog.like_user.all %}
<i class ="fas fa-heart" style="color:red; font-size:17px;"></i>
{% else %}
<i class ="far fa-heart" style="color:red; font-size:17px;"></i>
{% endif %}
</button>
<span>
{{ blog.like_user.all|length}} 명이 이 글을 좋아합니다.
</span>
</form>
</div>

<div>
<form action="{% url 'bookmark' blog.id %}" method='POST'>
{% csrf_token %}
<button style="border: none">
{% if user in blog.bookmark_user.all %}
<i class ="fas fa-solid fa-bookmark" style="color: black; font-size:17px;"></i>
{% else %}
<i class ="far fa-regular fa-bookmark" style="color: black; font-size:17px;"></i>
{% endif %}
</button>
</form>
</div>

<a href="{% url 'update' blog.id %}">글 수정하기</a>
<br>
<a href="{% url 'delete' blog.id %}">글 삭제하기</a>
{% endblock %}
31 changes: 31 additions & 0 deletions kimyongwook/likeapp/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css"/>
{% block title %}
<title>Index</title>
{% endblock %}
</head>
<body>
<nav>
<a href="{% url 'create' %}">글 작성하기</a><i class="fas fa-heart"></i>
<a href="{% url 'read' %}">글 모아보기</a><i class="fas fa-heart-broken"></i>
{% if user.is_active %}
{{ user.username }} 님 안녕하세요 <a href="{% url 'logout' %}">로그아웃</a>
{% else %}
<a href="{% url 'signup' %}">회원가입</a>
<a href="{% url 'login' %}">로그인</a><i class="fas fa-sad-cry"></i>
{% endif %}
<a href="{% url 'mybookmark' %}">북마크</a>
</nav>
{% block content %}
<h1>Index Page</h1>
<hr>
{% endblock %}
</body>
</html>
14 changes: 14 additions & 0 deletions kimyongwook/likeapp/templates/mybookmark.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'index.html' %}

{% block content %}
<h1>bookmark Page</h1>
<hr>

{% if user.is_active %}
<h2>{{ user.username }}님의 작성글</h2>
{% for blogs in blogs %}
<h3><a href="{% url 'detail' blogs.id %}">{{ blogs.title }}</a></h3>
<hr>
{% endfor %}
{% endif %}
{% endblock %}
25 changes: 25 additions & 0 deletions kimyongwook/likeapp/templates/read.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends 'index.html' %}

{% block title %}
<title>Read</title>
{% endblock %}

{% block content %}
<h1>Read Page</h1>
<hr>
{% for blogs in blogs %}
<h3><a href="{% url 'detail' blogs.id %}">{{ blogs.title }}</a></h3>
<p>{{ blogs.pub_date }}</p>
<p>{{ blogs.body }}</p>
<form action="{% url 'bookmark' blogs.id %}" method='POST'>
{% csrf_token %}
<button style="border: none">
{% if user in blogs.bookmark_user.all %}
<i class ="fas fa-solid fa-bookmark" style="color: black; font-size:17px;"></i>
{% else %}
<i class ="far fa-regular fa-bookmark" style="color: black; font-size:17px;"></i>
{% endif %}
</button>
</form>
{% endfor %}
{% endblock %}
17 changes: 17 additions & 0 deletions kimyongwook/likeapp/templates/update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'index.html' %}

{% block title %}
<title>Update</title>
{% endblock %}

{% block content %}
<h1>Update Page</h1>
<hr>
<form method="POST">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<button type = "submit">작성</button>
</form>
{% endblock %}
3 changes: 3 additions & 0 deletions kimyongwook/likeapp/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
15 changes: 15 additions & 0 deletions kimyongwook/likeapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.urls import path
from likeapp import views

urlpatterns = [
path('', views.index, name='index'),
path('create/', views.create, name='create'),
path('read/', views.read, name = 'read'),
path('detail/<str:id>/', views.detail, name = 'detail'),
path('update/<str:id>/', views.update, name='update'),
path('delete/<str:id>/', views.delete, name = 'delete'),
path('like/<int:id>', views.like, name='like'),
path('bookmark/', views.bookmark, name='bookmark'),
path('mybookmark/', views.mybookmark, name='mybookmark'),
path('bookmark/<int:id>/', views.bookmark, name='bookmark'),
]
75 changes: 75 additions & 0 deletions kimyongwook/likeapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from django.shortcuts import render, redirect, get_object_or_404
from .forms import BlogForm
from django.utils import timezone
from .models import Blog

# Create your views here.

def index(request):
return render(request, 'index.html')

def create(request):
if request.method == 'POST':
form = BlogForm(request.POST)
if form.is_valid():
form = form.save(commit=False)
form.pub_date = timezone.now()
form.save()
return redirect('read')
else:
form = BlogForm
return render(request, 'create.html', {'form' : form})

def read(request):
blogs = Blog.objects.all()
return render(request, 'read.html', {'blogs' : blogs})

def detail(request, id):
blog = get_object_or_404(Blog, id = id)
return render(request, 'detail.html', {'blog' : blog})

def update(request, id):
blog = get_object_or_404(Blog, id = id)
if request.method == 'POST':
form = BlogForm(request.POST, instance=blog)
if form.is_valid():
form = form.save(commit = False)
form.pub_date = timezone.now()
form.save()
return redirect('read')
else:
form = BlogForm(instance=blog)
return render(request, 'update.html', {'form' : form})

def delete(request, id):
blog = get_object_or_404(Blog, id = id)
blog.delete()
return redirect('read')

def like(request, id):
if request.user.is_authenticated:
blog = get_object_or_404(Blog, id = id)
if blog.like_user.filter(id = request.user.id).exists():
blog.like_user.remove(request.user)
else:
blog.like_user.add(request.user)
return redirect('detail', id=id)
return redirect('login')



def bookmark(request, id):
if request.user.is_authenticated:
blog = get_object_or_404(Blog, id = id)
if blog.bookmark_user.filter(id = request.user.id).exists():
blog.bookmark_user.remove(request.user)
else:
blog.bookmark_user.add(request.user)
return redirect('detail', id=id)
return redirect('login')

def mybookmark(request):
if request.user.is_authenticated:
blogs = Blog.objects.filter(bookmark_user=request.user)
return render(request, 'mybookmark.html', {'blogs' : blogs})
return redirect('login')
Empty file added kimyongwook/likeprj/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added kimyongwook/likeprj/__pycache__/wsgi.cpython-311.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions kimyongwook/likeprj/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for likeprj project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'likeprj.settings')

application = get_asgi_application()
Loading