staticfiles
앱을 통해 정적 파일과 관련된 기능을 제공INSTALLED_APPS
에 djnago.contrib.staticfiles
가 포함되어 있는지 확인하기settings.py
에서 STATIC_URL
을 정의하기# settings.py
INSTALLED_APPS = [
'articles',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
my_app/static/sample_img.jpg
{% load static %}
<img src="{% static 'sample.jpg' %}" alt="sample image" height="350">
{% load %}
{% static '' %}
collectstatic
이 배포를 위해 정적 파일을 수집하는 디렉토리의 절대 경로# settings.py
STATIC_ROOT = BASE_DIR / 'staticfiles'
$ python manage.py collectstatic
app/static/
디렉토리 경로를 사용하는 것(기본 경로) 외에 추가적인 정적 파일 경로 목록을 정의하는 리스트# 작성 예시
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
# 작성 예시
STATIC_URL = '/static/'
articles/static/articles
경로에 이미지 파일 배치하기
static tag를 사용해 이미지 파일 출력하기
{% load static %}
<img src="{% static 'articles/sample_img_1.png' %}" alt="sample-image-1">
# settings.py
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
static/
경로에 이미지 파일 배치하기{% load static %}
<img src="{% static 'sample_img_2.png' %}" alt="sample-image-2">
STATIC_URL + static file 경로
로 설정됨FileField(upload_to=’’, storage=None, max_length=100, **options)
MEDIA_ROOT
, MEDIA_URL
설정upload_to
속성을 정의하여 업로드된 파일에 사용할 MEDIA_ROOT의 하위 경로를 지정 (선택사항)MEDIA_ROOT
는 STATIC_ROOT
와 반드시 다른 경로로 지정해야 함# settings.py
# media
MEDIA_ROOT = BASE_DIR / 'media'
# settings.py
# media
MEDIA_URL = '/media/'
settings.MEDIA_URL
settings.MEDIA_ROOT
# crud/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
# articles/models.py
from django.db import models
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
# ImageField 작성
image = models.ImageField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
blank
blank=True
가 있으면 form 유효성 검사에서 빈 값을 입력받을 수 있음null
null=True
로 설정 시 데이터 없음에 대한 표현에 “빈 문자열”과 “NULL” 2가지 모두 가능하게 됨$ pip install pillow
$ python manage.py makemigrations
$ python manage.py migrate
$ pip freeze > requirements.txt
<!-- article/create.hmtl -->
{% extends 'base.html' %}
{% block content %}
<h1>글작성</h1>
<hr>
<form action="{% url 'articles:create' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<input type="submit">
</form>
{% endblock content %}
# articles/views.py
def create(request):
if request.method == 'POST':
form = ArticleForm(request.POST, request.FILES)
...
article.image.url
- 업로드 파일의 경로article.image
- 업로드 파일의 파일 이름<!-- article/detail.hmtl -->
<img src="{{article.image.url}}">
<!-- article/detail.hmtl -->
{% if article.image %}
<img src="{{article.image.url}}">
{% endif %}
<!-- article/update.hmtl -->
{% extends 'base.html' %}
{% block content %}
<h1>글수정</h1>
<hr>
<form action="{% url 'articles:update' article.pk %}" enctype="multipart/form-data" method="POST">
...
# articles/views.py
def update(request, pk):
article = Article.objects.get(pk=pk)
if request.method == 'POST':
form = ArticleForm(request.POST, request.FILES, instance=article)