카테고리 없음
Django 와 sqlite3로 CRUD 구현하기
광82
2023. 7. 19. 08:33
반응형

c:/venvs/pyweb/Scripts/activate.bat

django-admin startproject dbtest

반응형




cd dbtest
python manage.py migrate


sqlite3 db.sqlite3
.table


.quit

from django.db import models
class MyBoard(models.Model):
myname = models.CharField(max_length=100)
mytitle = models.CharField(max_length=500)
mycontent = models.CharField(max_length=2000)
mydate = models.DateTimeFiled('data published')

from django.apps import AppConfig
class DbtestConfig(AppConfig):
name = 'dbtest'

'dbtest.apps.DbtestConfig',

# 모델 생성
python manage.py makemigrations dbtest
# 모델 확인
python manage.py sqlmigrate dbtest 0001
# 테이블 생성
python manage.py migrate
sqlite3 db.sqlite3
.table
.schema dbtest_myboard





<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello, Django with sqlite3</h1>
<table border="1">
<col width="50">
<col width="100">
<col width="500">
<col width="100">
<tr>
<th>번호</th>
<th>이름</th>
<th>제목</th>
<th>날짜</th>
</tr>
{% if not list %}
<tr>
<th colspan="4">----------작성된 글이 없습니다.----------</th>
</tr>
{% else %}
{% for data in list %}
<tr>
<td>{{data.id}}</td>
<td>{{data.myname}}</td>
<td><a href="#">{{data.mytitle}}</a></td>
<td>{{date.mydate|date:"Y-m-d"}}</td>
</tr>
{% endfor %}
{% endif %}
<tr>
<td colspan="4" align="right">
<input type="button" value="글작성" onclick="location.href=''" />
</td>
</tr>
</table>
</body>
</html>

from django.shortcuts import render
from dbtest.models import MyBoard
def index(request):
return render(request, 'index.html', {'list': MyBoard.objects.all()})

from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]

python manage.py runserver

python manage.py shell

from dbtest.models import MyBoard
from django.utils import timezone

test = MyBoard(myname='admin', mytitle='test', mycontent='test1234', mydate=timezone.now())
test.save()
MyBoard.objects.all()
exit()

반응형