728x90
컴퓨터를 껏다가 켰다면 이전에 설정했던 가상황경에 다시 들어가야합니다
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()
728x90
'Python > Django' 카테고리의 다른 글
[Django] 장고 이용하여 페이지 구성하기 (0) | 2022.05.18 |
---|---|
[Django] 장고 이용하여 페이지 구성하기02 (0) | 2022.05.17 |
[Django] 장고 이용하여 페이지 구성하기01 (0) | 2022.05.16 |
[Django] 설치부터 기본 세팅(VSCODE) (0) | 2022.05.15 |