WEB/Django prac 181

[Django prac][Static files] 6. media 사용자가 업로드 한 사진 받아서 db에 저장

# articles > templates > articles > create.html 더보기 Create {% csrf_token %} {{ form.as_p }} [back] # articles > views.py form = ArticleForm(request.POST, request.FILES) 더보기 from django.shortcuts import render, redirect from .models import Article from .forms import ArticleForm # Create your views here. def index(request): articles = Article.objects.all() context = { 'articles': articles, } retur..

WEB/Django prac 2024.03.29

[Django prac][Static files] 5. media 사용자가 업로드 한 사진 받기

# Pillow 설치하기 media field 기능을 사용하려면, 이것이 꼭 필요하다 pip install Pillow pip list 이 명령어로 설치된 목록을 확인해볼 수 있다. pip freeze > requirements.txt # migration python manage.py makemigrations python manage.py migrate # 3번 설계도 구경 # db 열어서 확인 # 서버 실행하기 python manage.py runserver 이미지를 업로드 할 수 있는 칸이 잘 생겼다

WEB/Django prac 2024.03.29

[Django prac][Static files] 4. media 사용자가 업로드 한 사진 받기

# crud > settings.py 더보기 """ Django settings for crud project. Generated by 'django-admin startproject' using Django 4.2.11. For more information on this file, see https://docs.djangoproject.com/en/4.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ from pathlib import Path # Build paths inside the project like this: BA..

WEB/Django prac 2024.03.29

[Django prac][Static files] 3. 이미지 넣기

# articles > templates > articles > index.html 더보기 {% load static %} Articles CREATE {% for article in articles %} 글 번호: {{ article.pk }} 글 제목: {{ article.title }} 글 내용: {{ article.content }} {% endfor %} # 서버 실행해서 확인해보기 python manage.py runserver # crud > settings.py 이거 등록되어 있는지 확인해보기 # static 폴더 하나 만들기 # 두번째 이미지 파일 위치 옮기기 articles, crud, static 폴더가 같은 선상에 있어야한다. # crud > settings.py 두번째 파일 디렉토..

WEB/Django prac 2024.03.29

[Django prac][Form] 12. Widget 응용

# articles > forms 더보기 from django import forms from .models import Article # 위젯 미적용 버전 class ArticleForm(forms.ModelForm): class Meta: model = Article fields = '__all__' # 위젯 적용 버전 class ArticleForm(forms.ModelForm): title = forms.CharField( label='제목', widget=forms.TextInput( attrs={ 'class': 'my-title', 'placeholder': 'Enter the title', } ) ) content = forms.CharField( label='내용', widget=form..

WEB/Django prac 2024.03.29

[Django prac][Form] 11. update

기존 edit + 기존 create => 새로운 update 함수 만들기 # articles > views.py edit 함수만 주석처리 더보기 from django.shortcuts import render, redirect from .models import Article from .forms import ArticleForm # Create your views here. def index(request): articles = Article.objects.all() context = { 'articles': articles, } return render(request, 'articles/index.html', context) def detail(request, pk): article = Article..

WEB/Django prac 2024.03.29

[Django prac][Form] 8. Handling HTTP requests

기존 new + 기존 create를 해서 새로 create 하나 만들기 # articles > templates > articles > views.py 더보기 from django.shortcuts import render, redirect from .models import Article from . forms import ArticleForm def index(request): articles = Article.objects.all() context = { 'articles': articles, } return render(request, 'articles/index.html', context) def detail(request, pk): article = Article.objects.get(pk=p..

WEB/Django prac 2024.03.28