K-디지털트레이닝(빅데이터) 13일차
-
pip install django
-
django-admin startproject config .
-
버킷, iam, 파라미터그룹, db만들기
- config/settings.py
알아서 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',
- 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])
- python manage.py makemigrations post
- python manage.py migrate post
- 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)
- 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})
- 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'),
]
- config/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("post.urls")),
]
- templates 폴더 만들고 base.html 만든다
settings.py
'DIRS': [os.path.join(BASE_DIR, 'templates')],
- static 폴더 생성 하고 안에 넣기
- settings.py
STATICFILES_DIRS = (os.path.join('static'),)
- 메인에 들어갈거 말고 나머지는 다 리스트로 넣는다
cdn 적고 중복코드 제거
{% ' ' %} 적기(img, css, js)
list.html에서for문 쓰고 객체 불러오기
Author And Source
이 문제에 관하여(K-디지털트레이닝(빅데이터) 13일차), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@y7y1h13/K-디지털트레이닝빅데이터-12일차-8u5m1bob저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)