320x100
#첫번째 파일
mysite > cars > views.py
from django.shortcuts import render, redirect
from django.urls import reverse
from .forms import ReviewForm
# Create your views here.
def rental_review(request):
# POST REQUEST --> form contents --> thank_you
if request.method == 'POST':
form = ReviewForm(request.POST)
if form.is_valid():
#{'first_name':'Jose', }
print(form.cleaned_data)
return redirect(reverse('cars:thank_you'))
# ELSE, RENDER Form
else:
form = ReviewForm()
return render(request, 'cars/rental_review.html', context = {'form':form})
def thank_you(request):
return render(request, 'cars/thank_you.html')
# 두번째 파일
mysite > cars > templates > cars > rental_review.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">
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>
<title>rental_review.html</title>
</head>
<body>
<h1>Rental review form</h1>
<div class=container>
<form method="POST">
{% csrf_token %}
{% comment %}
as_p : p태그로 감싼 효과다.
as_table: table효과다
as_ul : ul태그로 감싼 효과다
{% endcomment %}
{% comment %} {{form.as_p}} {% endcomment %}
{% for field in form %}
<div class='mb-3'>
{{field.label_tag}}
</div>
{{field}}
{% endfor %}
<input type='submit'>
</form>
</div>
</body>
</html>
# 세번째 파일
mysite > cars > forms.py
from django import forms
class ReviewForm(forms.Form):
first_name = forms.CharField(label = 'First Name', max_length=100)
last_name = forms.CharField(label = 'Last Name', max_length=100)
email = forms.EmailField(label = 'Email')
review = forms.CharField(label = 'Please write your review here')
결과적으로 이렇게 셋팅해놓고
vscode 터미널에
python manage.py runserver
하고
http://127.0.0.1:8000/cars/rental_review/
로 접속하면 변화한 형태가 보인다.
300x250
'개발일지 > 임시카테고리' 카테고리의 다른 글
Django - ModelForms (0) | 2022.10.01 |
---|---|
Django - 위젯과 스타일링 (0) | 2022.10.01 |
Django - form class 기초 (다음 포스팅과 연결될 예정) (1) | 2022.10.01 |
Django - admin superuser생성 및 기본 다루기 (0) | 2022.10.01 |
Django - 기본사이클 - 모델과 웹사이트2 (1) | 2022.09.30 |