pip install django
django-admin startproject config .
버킷, iam, 파라미터그룹, db만들기
알아서 database랑 aws 추가하기
pip install boto3
pip install django-storages
pip install pymysql
python manage.py startapp post
python manage.py createsuperuser
python manage.py collectstatic
pip install pillow
settings.py
'post',
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
class Category(models.Model):
name = models.CharField(max_length=200, db_index=True)
meta_description = models.TextField(blank=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
class Meta:
ordering = ['name'] # 이름으로 정렬
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('post:post_in_category', args=[self.slug])
class Post(models.Model):
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, related_name='posts')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_posts')
title = models.CharField(max_length=200, db_index=True)
photo = models.ImageField(upload_to='posts/%Y/%m/%d', blank=True)
text = models.TextField(blank=True)
description = models.TextField(blank=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-created', '-updated']
def __str__(self):
return self.author.username + " " + self.created.strftime('%Y-%m-%d %H:%M:%S')
def get_absolute_url(self):
return reverse('post:post_detail', args=[self.id])
from django.contrib import admin
from .models import *
class CategoryAdmin(admin.ModelAdmin):
list_display = ['id', 'name', 'slug']
prepopulated_fields = {'slug': ['name']}
admin.site.register(Category, CategoryAdmin)
class PostAdmin(admin.ModelAdmin):
list_display = ['id', 'title', 'author', 'created', 'updated']
list_filter = ['author', 'created', 'updated']
admin.site.register(Post, PostAdmin)
from django.shortcuts import render
from .models import Post
def post_list(request):
posts = Post.object.all()
return render(request, 'post/list.html', {'posts':posts})
from django.urls import path
from .views import *
from .models import Post
app_name = 'post'
urlpatterns = [
path('', post_list, name='post_list'),
]
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("post.urls")),
]
settings.py
'DIRS': [os.path.join(BASE_DIR, 'templates')],
STATICFILES_DIRS = (os.path.join('static'),)
cdn 적고 중복코드 제거
{% ' ' %} 적기(img, css, js)
list.html에서for문 쓰고 객체 불러오기