Django III에서 다루게 될 내용은 다음과 같습니다.
4-1 Django custom user profile
4-2 User-submitted content
4-3 Finishing touches
4-2 User-submitted content
(1) Creating a vote model
Vote model을 models.py에 추가
다음과 같이 코드를 추가합니다.
mysite > main > models.py
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-1.png)
Migrations 수행
다음 명령을 수행합니다.
>python manage.py makemigrations
>python manage.py migrate
Vote model을 폼에 연결
다음과 같이 코드를 추가합니다.
mysite > main > forms.py
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-2.png)
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-3.png)
VoteForm을 product 함수에 추가
다음과 같이 코드를 추가합니다.
mysite > main > views.py
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-4.png)
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-5-1024x73.png)
(2) Adding the vote form to the products page
Bootstrap collapse를 product.html에 추가
다음과 같이 코드를 추가합니다.
mysite > main > templates > main > products.html
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-6-1024x69.png)
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-7.png)
script를 product.html에 추가
다음과 같이 코드를 추가합니다.
mysite > main > templates > main > products.html
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-8.png)
style를 products.html에 추가
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-9.png)
브라우저에서 확인해 봅니다.
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-10.png)
(3) Handling the vote form submission
views.py에 product 기능 업데이트
다음과 같이 코드를 추가합니다.
mysite > main > views.py
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-11.png)
브라우저에서 확인해 봅니다.
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-12.png)
(4) Displaying the product scores
models.py에 Product model 업데이트
다음과 같이 코드를 추가합니다.
mysite > main > models.py
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-13.png)
Function을 Vote 모델에 추가
다음과 같이 코드를 추가합니다.
mysite > main > models.py
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-14.png)
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-15.png)
Migrations 수행
다음 명령을 수행합니다.
>python manage.py makemigrations
>python manage.py migrate
views.py에 products view 업데이트
다음과 같이 코드를 추가합니다.
mysite > main > views.py
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-16.png)
products.html에서 products에 user score 추가
다음과 같이 코드를 추가합니다.
mysite > main > views.py
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-17-1024x74.png)
home.html에서 products에 user score 추가
mysite > main > templates > main > home.html
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-18.png)
브라우저에서 확인해 봅니다.
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-19.png)
브라우저에서 확인해 봅니다.
![](https://makebct.net/wp-content/uploads/2023/06/django4-2-20.png)