320x100
# 첫번째 파일
mysite > cars > models.py
from django.db import models
# 카멜 라이팅 방식에 유의 및 단어에 유의
# 최소허용값, 최대허용값
from django.core.validators import MinValueValidator, MaxValueValidator
# Create your models here.
class Review(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
stars = models.IntegerField(validators = [MinValueValidator(1), MaxValueValidator(5)])
# 두번째 파일
mysite > cars > templates > cars > rental_review.html
{% load static %}
<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>
<link rel="stylesheet" href="{% static 'cars/custom.css' %}"
<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}}
{% comment %} 적합한 에러 메세지를 출력해준다. {% endcomment %}
{{field.errors}}
{% endfor %}
<input type='submit'>
</form>
</div>
</body>
</html>
# 세번째 파일
mysite > cars > admin.py
from django.contrib import admin
from .models import Review
# Register your models here.
admin.site.register(Review)
# 네번째 파일
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():
form.save()
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 > forms.py
from django import forms
from .models import Review
from django.forms import ModelForm
'''
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',
widget=forms.Textarea(attrs={
'class': 'myform',
'rows':'2',
'cols':'2'}))
'''
class ReviewForm(ModelForm):
class Meta:
model = Review
fields = '__all__' # pass in all model fields as form fields
labels = {
'first_name': 'YOUR FIRST NAME',
'last_name': 'Last Name',
'stars':'Rating'
}
error_messages = {
'stars': {
'min_value':'min value is 1',
'max_value': 'max value is 5'
}
}
300x250
'개발일지 > Django' 카테고리의 다른 글
Django - cbv formview (0) | 2022.10.01 |
---|---|
Django - cbv template view (0) | 2022.10.01 |
Django - ModelForms (0) | 2022.10.01 |
Django - 위젯과 스타일링 (0) | 2022.10.01 |
Django - form 탬플릿 랜더링 (0) | 2022.10.01 |