12번째 줄: |
12번째 줄: |
| | | |
| === board.urls에 추가 === | | === board.urls에 추가 === |
− | urlpatterns 안에 다음을 추가한다.<syntaxhighlight lang="python"> | + | urls.py 안에 필요한 기능을 다 담아주어야 한다. urlpatterns 안에 다음을 추가한다. |
| + | |
| + | (뷰의 설정이나, import방식에 따라 뷰를 불러오는 방식이 달라질 수 있다.) |
| + | {| class="wikitable" |
| + | !일반적으로 코드를 짜는 경우 |
| + | !제네릭 뷰를 사용하는 경우 |
| + | |- |
| + | |<syntaxhighlight lang="python"> |
| + | from django.urls import path |
| + | from . import views #해당 앱의 뷰를 불러온다. |
| + | |
| + | app_name = 'board' |
| + | |
| urlpatterns = [ | | urlpatterns = [ |
− | path('create/', views.create, name='create'), | + | ... |
| + | path('create/', views.create, name='create'),#글의 작성화면 |
| + | ... |
| ] | | ] |
| + | </syntaxhighlight> |
| + | |함수명을 바꾸어주어야 한다. |
| + | views.클래스뷰명.as_view()), 형태로. |
| + | |
| + | 클래스형 뷰임을 지정해주기 위해. |
| + | |} |
| + | |
| + | == form 작성 == |
| + | board/forms.py를 만든 후 다음과 같이 입력한다. |
| + | |
| + | 자동으로 에러검사도 해주고 편리해 사용하지 않을 이유가 없어 form을 적극적으로 활용한다.<syntaxhighlight lang="python"> |
| + | from django import forms |
| + | from .models import Writing # 폼을 적용할 모델을 불러온다. |
| | | |
| + | class WritingForm(forms.ModelForm): |
| + | class Meta: |
| + | model = Writing # 사용할 모델 |
| + | fields = ['subject', 'content'] # 폼으로 입력할 필드를 입력해준다. |
| + | # fields에 '__all__'을 따옴표까지 함께 넣어주면 모든 필드를 가져오라는 명령이 된다. |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
40번째 줄: |
72번째 줄: |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | == form 작성 == | + | == template 작성 == |
− | board/forms.py를 만든 후 다음과 같이 입력한다.<syntaxhighlight lang="python">
| |
− | from django import forms
| |
− | from .models import Writing # 폼을 적용할 모델을 불러온다.
| |
| | | |
− | class WritingForm(forms.ModelForm):
| |
− | class Meta:
| |
− | model = Writing # 사용할 모델
| |
− | fields = ['subject', 'content'] # 폼으로 입력할 필드를 입력해준다.
| |
− | # fields에 '__all__'을 따옴표까지 함께 넣어주면 모든 필드를 가져오라는 명령이 된다.
| |
− | </syntaxhighlight>
| |
− |
| |
− | == template 작성 ==
| |
| board/create.html을 만들고 body 안에 다음이 들어가게 한다.<syntaxhighlight lang="html+django"> | | board/create.html을 만들고 body 안에 다음이 들어가게 한다.<syntaxhighlight lang="html+django"> |
| <body> | | <body> |