Skip to content

Commit

Permalink
D20 | 190129 PM | CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunwoo-song committed Jan 29, 2019
1 parent cbee8c4 commit 0d0155a
Show file tree
Hide file tree
Showing 40 changed files with 708 additions and 7 deletions.
17 changes: 17 additions & 0 deletions django/crud/posts/templates/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Post detail</h1>
<h2>Title : {{ post.title }}</h2>
<p>content : {{ post.content }}</p>
<a href="/posts/">List</a>
<a href="/posts/{{ post.pk}}/edit/">edit</a>
<a href="/posts/{{ post.pk}}/delete/">Delete</a>
</body>
</html>
18 changes: 18 additions & 0 deletions django/crud/posts/templates/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Post Edit</h1>
<form action="/posts/{{ post.pk }}/update/" method="post">
{% csrf_token %}
<input type="text" name="title" value="{{ post.title }}"/>
<input type="text" name="content" value="{{ post.content }}"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
3 changes: 2 additions & 1 deletion django/crud/posts/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
</head>
<body>
<h1>Post Index</h1>
<a href="/posts/new/">New</a>
<ul>
{% for post in posts %}
<li> {{ post.title }} - {{ post.content }}</li>
<li> <a href="/posts/{{post.pk}}/">{{ post.title }} - {{ post.content }}</a></li>
{% endfor %}
</ul>
</body>
Expand Down
3 changes: 2 additions & 1 deletion django/crud/posts/templates/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<title>Document</title>
</head>
<body>
<form action="/posts/create/" method="get">
<form action="/posts/create/" method="post">
{% csrf_token %}
<input type="text" name="title"/>
<input type="text" name="content"/>
<input type="submit" value="Submit"/>
Expand Down
7 changes: 7 additions & 0 deletions django/crud/posts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from . import views

urlpatterns = [
path('naver/<str:q>/', views.naver),
path('github/<str:username>/', views.github),
path('', views.index),
path('new/', views.new),
path('create/', views.create),
path('<int:post_id>/', views.detail),
path('<int:post_id>/delete/', views.delete),
path('<int:post_id>/edit/', views.edit),
path('<int:post_id>/update/', views.update),

]
41 changes: 36 additions & 5 deletions django/crud/posts/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from .models import Post

# Create your views here.
def new(request):
return render(request, 'new.html')

def create(request):
title = request.GET.get('title')
content = request.GET.get('content')
title = request.POST.get('title')
content = request.POST.get('content')

post = Post(title=title, content=content)
post.save()

return render(request, 'create.html')
return redirect(f'/posts/{post.pk}/')


def index(request):
# All Post
posts = Post.objects.all()

return render(request, 'index.html', {'posts':posts})
return render(request, 'index.html', {'posts':posts})

def detail(request, post_id):
post = Post.objects.get(pk=post_id)

return render(request, 'detail.html', {'post':post})


def naver(request, q):
return redirect(f'https://search.naver.com/search.naver?query={q}')


def github(request, username):
return redirect(f'https://github.com/{username}')


def delete(request, post_id):
# 삭제하는 코드
post = Post.objects.get(pk=post_id)
post.delete()
return redirect("/posts/")

def edit(request, post_id):
post = Post.objects.get(pk=post_id)
return render(request, 'edit.html', {'post':post})

def update(request, post_id):
post = Post.objects.get(pk=post_id)
post.title = request.POST.get('title')
post.content = request.POST.get('content')
post.save()
return redirect(f'/posts/{post_id}/')
Empty file added django/hw19/hw19/__init__.py
Empty file.
121 changes: 121 additions & 0 deletions django/hw19/hw19/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""
Django settings for hw19 project.
Generated by 'django-admin startproject' using Django 2.1.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*v-6t06fb9*dwrru8q6889517ck3mjgg1rc@@m)d5z5ehu3((8'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['playground-tot23.c9users.io']


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'students.apps.StudentsConfig',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'hw19.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'hw19.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'ko-kr'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'
25 changes: 25 additions & 0 deletions django/hw19/hw19/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""hw19 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import include

urlpatterns = [
path('students/', include('students.urls')),
path('admin/', admin.site.urls),


]
16 changes: 16 additions & 0 deletions django/hw19/hw19/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for hw19 project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()
16 changes: 16 additions & 0 deletions django/hw19/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python
import os
import sys


if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hw19.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
Empty file.
10 changes: 10 additions & 0 deletions django/hw19/students/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib import admin
from .models import Student
# Register your models here.


# class StudentAdmin(admin.ModelAdmin):
# list_display = ('name', 'email', 'birthday', 'age',)


admin.site.register(Student)
5 changes: 5 additions & 0 deletions django/hw19/students/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class StudentsConfig(AppConfig):
name = 'students'
24 changes: 24 additions & 0 deletions django/hw19/students/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.1.5 on 2019-01-29 05:54

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Student',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('email', models.CharField(max_length=100)),
('birthday', models.DateField()),
('age', models.IntegerField()),
],
),
]
Empty file.
11 changes: 11 additions & 0 deletions django/hw19/students/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models

# Create your models here.
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
birthday = models.DateField()
age = models.IntegerField()

def __str__(self):
return self.name
12 changes: 12 additions & 0 deletions django/hw19/students/templates/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>성공적으로 student를 생성했습니다!</h1>
</body>
</html>
Loading

0 comments on commit 0d0155a

Please sign in to comment.