Skip to content

Commit

Permalink
D21 |190130| CRUD-PLUS
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunwoo-song committed Jan 30, 2019
1 parent 0d0155a commit c79c02c
Show file tree
Hide file tree
Showing 20 changed files with 374 additions and 6 deletions.
Empty file.
121 changes: 121 additions & 0 deletions django/crud-plus/crud/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""
Django settings for crud 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 = 'c$^ij+=y6adaw4ahsd++bteqzau-u(ar)g8*+-*8nxmk67!9ts'

# 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',
'posts.apps.PostsConfig',
]

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 = 'crud.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 = 'crud.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 = 'en-us'

TIME_ZONE = 'UTC'

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/'
24 changes: 24 additions & 0 deletions django/crud-plus/crud/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""crud 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, include
from posts import views

urlpatterns = [
path('posts/', include('posts.urls')),
path('create/', views.create),
path('admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions django/crud-plus/crud/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for crud 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', 'crud.settings')

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

if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crud.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.
13 changes: 13 additions & 0 deletions django/crud-plus/posts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib import admin
from .models import Post



class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'content', )




# Register your models here.
admin.site.register(Post, PostAdmin)
5 changes: 5 additions & 0 deletions django/crud-plus/posts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class PostsConfig(AppConfig):
name = 'posts'
22 changes: 22 additions & 0 deletions django/crud-plus/posts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.1.5 on 2019-01-28 02:43

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('content', models.TextField()),
],
),
]
Empty file.
9 changes: 9 additions & 0 deletions django/crud-plus/posts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models

# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()

def __str__(self):
return self.title
17 changes: 17 additions & 0 deletions django/crud-plus/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="{% url 'posts:list' %}">List</a>
<a href="{% url 'posts:edit' post.pk %}">edit</a>
<a href="{% url 'posts:delete' post.pk %}">Delete</a>
</body>
</html>
18 changes: 18 additions & 0 deletions django/crud-plus/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="{% url 'posts:update' post.pk %}" 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>
18 changes: 18 additions & 0 deletions django/crud-plus/posts/templates/index.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 Index</h1>
<a href="{% url 'posts:new'%}">New</a>
<ul>
{% for post in posts %}
<li> <a href="{% url 'posts:detail' post.pk %}">{{ post.title }} - {{ post.content }}</a></li>
{% endfor %}
</ul>
</body>
</html>
17 changes: 17 additions & 0 deletions django/crud-plus/posts/templates/new.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>
<form action="{% url 'posts:create' %}" method="post">
{% csrf_token %}
<input type="text" name="title"/>
<input type="text" name="content"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
3 changes: 3 additions & 0 deletions django/crud-plus/posts/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.
18 changes: 18 additions & 0 deletions django/crud-plus/posts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.urls import path
from . import views

app_name = 'posts'

urlpatterns = [

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

]
52 changes: 52 additions & 0 deletions django/crud-plus/posts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.POST.get('title')
content = request.POST.get('content')

post = Post(title=title, content=content)
post.save()
return redirect('posts:detail', post.pk)


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

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:list")

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('posts:detail', post.pk)
Loading

0 comments on commit c79c02c

Please sign in to comment.