K-디지털트레이닝(빅데이터) 13일차

  1. pip install django

  2. django-admin startproject config .

  3. 버킷, iam, 파라미터그룹, db만들기

  1. config/settings.py
알아서 database랑 aws 추가하기
  1. pip install boto3

  2. pip install django-storages

  3. pip install pymysql

  4. python manage.py startapp post

  5. python manage.py createsuperuser

  6. python manage.py collectstatic

  7. pip install pillow

  8. settings.py

'post',
  1. models.py
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])
  1. python manage.py makemigrations post
  2. python manage.py migrate post
  1. admin.py
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)
  1. views.py

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})
  1. post/urls.py
from django.urls import path
from .views import *
from .models import Post

app_name = 'post'

urlpatterns = [
    path('', post_list, name='post_list'),

]
  1. config/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("post.urls")),
]
  1. templates 폴더 만들고 base.html 만든다
settings.py

        'DIRS': [os.path.join(BASE_DIR, 'templates')],
  1. static 폴더 생성 하고 안에 넣기
  1. settings.py
STATICFILES_DIRS = (os.path.join('static'),)
  1. 메인에 들어갈거 말고 나머지는 다 리스트로 넣는다

cdn 적고 중복코드 제거
{% ' ' %} 적기(img, css, js)

list.html에서for문 쓰고 객체 불러오기

좋은 웹페이지 즐겨찾기