Django45 [Django] <var> 📌 : url을 나타낼 때, 변수처럼 사용 가능. (urls.py) from django.urls import path from . import views urlpatterns = [ path('index/', views.index) ] → index/ 는 의 값에 따라 index/123, index/234, index/345 등으로 바뀜. (views.py) from django.shortcuts import render from .models import Board # Create your views here. def index(request, var): print(var) b = Board.objects.all() context = { "bset" : b } return render(request,.. 2022. 12. 19. [Django] DB 가져와서 CRUD(3) - DB 읽고 화면에 출력하기(R) DB 읽고 화면에 출력하기 📌 index.html에 표 띄우기 (books/views.py) from django.shortcuts import render from .models import Books # Create your views here. def index(request): b = Books.objects.all() context = { "bset" : b } return render(request, "books/index.html", context) (templates/books/index.html) INDEX PAGE NO TITLE WRITER HIT {% for i in bset %} {{ forloop.counter }} {{ i.name }} {{ i.writer }} {{ i.hi.. 2022. 12. 19. [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. 이전 1 2 3 4 5 6 다음