강좌 10 완료

This commit is contained in:
2017-06-02 17:47:33 +09:00
parent 0b808fa55b
commit 9894fbe8ff
31 changed files with 256 additions and 4 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
**/__pycache__/
**/upload/
**/migrations/
*.sh

View File

@@ -1,5 +1,6 @@
FROM python:3.5
ENV PYTHONUNBUFFERED 1
# RUN useradd -u 1000 -g 1000 mjjo
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/

Binary file not shown.

0
code/photos/__init__.py Normal file
View File

6
code/photos/admin.py Executable file
View File

@@ -0,0 +1,6 @@
from django.contrib import admin
# Register your models here.
from .models import Photo
admin.site.register(Photo)

5
code/photos/apps.py Normal file
View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class PhotosConfig(AppConfig):
name = 'photos'

8
code/photos/forms.py Executable file
View File

@@ -0,0 +1,8 @@
from __future__ import unicode_literals
from django import forms
from .models import Photo
class PhotoForm(forms.ModelForm):
class Meta:
model = Photo
fields = ('image', 'content')

23
code/photos/models.py Executable file
View File

@@ -0,0 +1,23 @@
from django.db import models
from django.core.urlresolvers import reverse_lazy
from django.conf import settings
# Create your models here.
class Photo(models.Model):
image = models.ImageField(upload_to='org/%Y')
filtered_image = models.ImageField(upload_to='filtered/%Y')
content = models.TextField(max_length=500, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
class Meta:
ordering = ('-pk', )
def get_absolute_url(self):
url = reverse_lazy('detail', kwargs={'pk': self.pk})
return url
def delete(self, *args, **kwargs):
self.image.delete()
self.filtered_image.delete()
super(Photo, self).delete(*args, **kwargs)

11
code/photos/templates/edit.html Executable file
View File

@@ -0,0 +1,11 @@
{% extends 'layout.html' %}
{% block content %}
<form method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<p><button type="submit">저장</button></p>
</form>
{% endblock %}

View File

@@ -0,0 +1,16 @@
{% load staticfiles %}
<!DOCTYPE html>
<html lang="ko">
<head>
<title>{% block page_title %}Pystagram{% endblock %}</title>
<meta charset="utf-8">
<script type="text/javascript" src="{% static 'js/jquery-3.2.1.min.js' %}" ></script>
</head>
<body>
{% include 'top_navi.html' %}
{% block content %}{% endblock %}
</body>
</html>

View File

@@ -0,0 +1,17 @@
{% extends "layout.html" %}
{% block content %}
{% if form.errors %}
<p>ID나 비밀번호가 일치하지 않습니다.</p>
{% endif %}
<form method="POST" action="{% url 'login' %}">
{% csrf_token %}
<input type="hidden" name="next" value="" />
{{ form.as_p }}
<button type="submit">로그인</button>
</form>
{% endblock %}

View File

@@ -0,0 +1,7 @@
<div>
{% if user.is_authenticated %}
hello {{ user.username }}! <a href="{{ logout_url }}">로그아웃</a>
{% else %}
<a href="{{ login_url }}">로그인</a>
{% endif %}
</div>

3
code/photos/tests.py Executable file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

55
code/photos/views.py Executable file
View File

@@ -0,0 +1,55 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from .models import Photo
from .forms import PhotoForm
from django.conf import settings
def general_ctx(request):
ctx = {
'login_url': settings.LOGIN_URL,
'logout_url': '/accounts/logout/',
'user': request.user,
}
return ctx
def hello(request):
return HttpResponse('안녕하세요!')
def detail(request, pk, hidden=False):
photo = get_object_or_404(Photo, pk=pk)
msg = '<p>Photo No {}</p>\n'.format(pk)
msg += '<p>url : {url}</p>\n'.format(url=photo.image.url)
msg += '<p><img src="{url}">\n</p>'.format(url=photo.image.url)
msg += '<p>{}</p>\n'.format(photo.content)
if hidden is True:
msg += 'hidden 이지롱\n'
return HttpResponse(msg)
@login_required
def create(request):
if request.method == "GET":
form = PhotoForm()
elif request.method == "POST":
form = PhotoForm(request.POST, request.FILES)
if form.is_valid():
obj = form.save(commit=False)
obj.user = request.user
obj.save()
return redirect(obj)
ctx = general_ctx(request)
ctx['form'] = form
print(ctx['user'])
return render(request, 'edit.html', ctx)

View File

3
code/profiles/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
code/profiles/apps.py Normal file
View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class ProfilesConfig(AppConfig):
name = 'profiles'

3
code/profiles/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,14 @@
{% extends 'layout.html' %}
{% block content %}
<h1>{{ user_name }}님의 프로필 페이지</h1>
<ul>
{% for photo in user.photo_set.all %}
<li><img src="{{ photo.image.url }}" style="max-width: 250px"/><span>{{ photo.content }}</span></li>
{% empty %}
<li>게시한 사진이 없습니다.</li>
{% endfor %}
</ul>
{% endblock %}

3
code/profiles/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

7
code/profiles/urls.py Executable file
View File

@@ -0,0 +1,7 @@
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<username>[\w.@+-]+)/$', views.profile, name='profile'),
]

28
code/profiles/views.py Executable file
View File

@@ -0,0 +1,28 @@
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.contrib.auth import get_user_model
from django.conf import settings
def general_ctx(request):
ctx = {
'login_url': settings.LOGIN_URL,
'logout_url': '/accounts/logout/',
'user': request.user,
}
return ctx
# Create your views here.
def profile(request, username):
User = get_user_model()
user = get_object_or_404(User, username=username)
photos = user.photo_set.order_by('-pk').all()
ctx = general_ctx(request)
ctx['user'] = user
ctx['photos'] = photos
print(ctx)
return render(request, 'profile.html', ctx)

View File

@@ -37,6 +37,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'photos',
'profiles',
]
MIDDLEWARE = [
@@ -54,7 +56,7 @@ ROOT_URLCONF = 'pystagram.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
@@ -108,7 +110,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Seoul'
USE_I18N = True
@@ -120,4 +122,13 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_URL = '/asset/'
STATIC_ROOT = os.path.join(BASE_DIR, 'asset')
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload')
MEDIA_URL = '/upload/'
LOGIN_REDIRECT_URL = '/photos/upload/'

16
code/pystagram/urls.py Normal file → Executable file
View File

@@ -15,7 +15,23 @@ Including another URLconf
"""
from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.contrib.auth import views as auth_views
from django.conf.urls.static import static
from django.conf.urls import include
from photos import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/login/', auth_views.login, name='login', kwargs={'template_name': 'login.html'}),
url(r'^accounts/logout/', auth_views.logout, name='logout', kwargs={'next_page': settings.LOGIN_URL}),
url(r'^photos/(?P<pk>[0-9]+)/$', views.detail, name='detail'),
url(r'^photos/upload/$', views.create, name='create'),
url(r'^users/', include('profiles.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

4
code/static/js/jquery-3.2.1.min.js vendored Executable file

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,7 @@
pystagram:
image: django
container_name: pystagram
restart: always
external_links:
- mariadb
ports:

View File

@@ -1,3 +1,4 @@
Django
psycopg2
mysqlclient
mysqlclient
Pillow