본문 바로가기

분류 전체보기361

[Django] DB 가져와서 CRUD(2) - DB 생성 및 레코드 추가 DB Table 생성 📌 Table 생성 (books/models.py) from django.db import models # Create your models here. class Books(models.Model): name = models.CharField(max_length=100) writer = models.CharField(max_length=100) content = models.TextField() hit = models.IntegerField(default=0) def __str__(self): return self.name 📌 마이그레이션 python manage.py makemigrations python manage.py migrate admin 계정 생성 및 레코드 추가 📌 a.. 2022. 12. 19.
[Django] DB 가져와서 CRUD(1) - 기본 세팅 기본 세팅 📌 가상 환경 실행 후, Django project 실행 django-admin startproject config . 📌 'books' app 생성 python manage.py startapp books 📌 'books' app 등록 ① (config/settings.py) INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'books.apps.BooksConfig' ] 📌 url 분리 ① (config/urls.py) .. 2022. 12. 19.
[Django] DB 가져오기(+ Template Tag) HTML에 데이터 전달 📌 HTML에 데이터 전달 ① 생성한 app의 views.py에 context 인자 추가. # Book 테이블 전달. from django.shortcuts import render from .models import Book # Create your views here. def index(request): b = Book.objects.all() context = { "bset" : b } return render(request, "book/index.html", context); Template Tag 📌 Template Tag : HTML 문서에서 Python 구문 사용할 수 있게하는 태그. 📌 {{ }} : 객체나 변수, 함수 나타낼 때 사용. 📌 {% %} : 구문(if,.. 2022. 12. 12.
[Python/Django] 관리자 계정 생성하기 관리자 계정 생성하기 📌 관리자 계정 생성하기 python manage.py createsuperuser admin 사이트에 테이블 등록하기 및 레코드 CRUD 📌 admin 사이트에 테이블 등록하기 및 CRUD ① admin.py에서 테이블 등록. from django.contrib import admin from app이름.models import 테이블 # Register your models here. admin.site.register(테이블) ② 'python manage.py runserver' 입력해 서버 실행. ③ http://127.0.0.1:8000/admin/ 에 로그인 후, 생성한 테이블의 레코드 CRUD 가능. 2022. 12. 12.
[Django] python shell로 데이터 CRUD 가상 환경으로 python shell 실행하기 📌 가상 환경으로 python shell 실행하기 python manage.py shell 데이터 추가하기 📌 데이터 추가하기 // 'test' app의 Student 테이블 데이터 from test.models import Student st = Student(name="kim", age=30, subject="eng").save() st = Student(name="lee", age=26, subject="kor").save() st = Student(name="park", age=29, subject="math").save() 데이터 조회하기 📌 데이터 한 개 조회하기 s1 = Student.objects.get(name="kim") s1 // 확인 s.. 2022. 12. 12.
[Django] ORM과 DB 등록하기 ORM(Object-Relation Mapping) 📌 ORM(Object-Relation Mapping) : 객체(Object)와 관계형 데이터베이스(Relational)를 연결(Mapping)하는 것. 데이터베이스를 객체와 연결해 CRUD(Create Read Update Delete)할 때, SQL을 사용하지 않고 할 수 있음. 테이블 생성하기 📌 테이블 생성하기 ① app 생성. python manage.py startapp app이름 ② app의 models.py에 table 생성. # 예시) Student 테이블 생성. from django.db import models # Create your models here. class Student(models.Model): # table 사용하기 .. 2022. 12. 12.
[Django] HTML 파일로써 Client에게 응답 주기 HTML 파일로 Response 날리기 📌 Render( ) from django.shortcuts import render # Create your views here. def 함수 이름(request): return render(request, 'app이름/생성한 html파일 이름.html') Template 분리 📌 Template 분리 ① template 폴더 안에 app마다 폴더 생성. ② app별로 해당 app에 대한 html 파일 생성해서 관리. → 사용하지 않는 app 관리에 용이함. ③ config/settings.py 수정. ... TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [.. 2022. 12. 12.
[Django] path 및 앱 생성 명령어 path 📌 path([요청 url], [함수]) : 127.0.0.1:8000/ + 요청 url로 요청이 들어오면, 함수에서 처리함. 📄 config/urls.py """config URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/4.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based .. 2022. 12. 6.