Created
May 29, 2025 01:05
-
-
Save jacklight74/87b2290be000993221cae9f2971cdd0a to your computer and use it in GitHub Desktop.
server django question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>새로운 질문 만들기</title> | |
</head> | |
<body> | |
<script> | |
let choiceCount = 1; | |
function addChoice() { | |
const choicesDiv = document.getElementById('choices-container'); | |
const newChoiceDiv = document.createElement('div'); | |
newChoiceDiv.className = 'choice'; | |
newChoiceDiv.innerHTML = ` | |
<label>${++choiceCount}.</label> | |
<input type="text" name="choices" placeholder="선택지 입력" required> | |
<button type="button" onclick="removeChoice(this)">삭제</button> | |
`; | |
choicesDiv.appendChild(newChoiceDiv); | |
} | |
function removeChoice(button) { | |
const choiceDiv = button.parentElement; | |
choiceDiv.remove(); | |
} | |
function addWeeks(weeks) { | |
let date = new Date(); | |
date.setDate(date.getDate() + 7 * weeks); | |
const year = date.getFullYear(); | |
const month = String(date.getMonth() + 1).padStart(2, '0'); | |
const day = String(date.getDate()).padStart(2, '0'); | |
document.getElementById('expire_date').value = `${year}-${month}-${day}`; | |
console.log(date); | |
} | |
</script> | |
<h1>새로운 질문 만들기</h1> | |
<form method="post"> | |
{% csrf_token %} | |
<!-- 질문 입력 --> | |
<div class="form-group"> | |
<label for="question_text">질문</label> | |
<input type="text" id="question_text" name="question_text" class="form-control" required> | |
</div> | |
<!-- 마감일 입력 --> | |
<div class="form-group"> | |
<label for="expire_date">투표 마감일</label> | |
<input type="date" id="expire_date" name="expire_date" class="form-control" required> | |
<button type="button" onclick="addWeeks(1)" class="btn btn-secondary mt-2">+1주일</button> | |
</div><br> | |
<!-- 선택지 입력 --> | |
<div class="form-group"> | |
<label>선택지</label> | |
<div id="choices-container"> | |
<div class="input-group mb-2"> | |
<label>1.</label> | |
<input type="text" name="choices" class="form-control" required> | |
<button type="button" class="btn btn-danger" onclick="removeChoice(this)">삭제</button> | |
</div> | |
</div> | |
<button type="button" class="btn btn-secondary" onclick="addChoice()">선택지 추가</button> | |
</div> | |
<br> | |
<input type="submit" value="질문 만들기"> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponseRedirect, HttpRequest | |
from django.shortcuts import render, get_object_or_404 | |
from django.urls import reverse | |
from vote.models import Question | |
# Create your views here. | |
def index(request: HttpRequest): | |
question_list = Question.objects.all() | |
# 메세지 표시 (optional, create 페이지에서 질문 만든경우) | |
created = request.GET.get("created", False) | |
return render( | |
request, "index.html", | |
{"question_list": question_list, "created": created} | |
) | |
def detail(request: HttpRequest, question_id: str): | |
question = get_object_or_404(Question, pk=question_id) | |
return render(request, "detail.html", {"question": question}) | |
def vote(request: HttpRequest, question_id: str): | |
question = get_object_or_404(Question, pk=question_id) | |
selected_choice = question.choice_set.get(pk=request.POST["choice"]) | |
selected_choice.votes += 1 | |
selected_choice.save() | |
return HttpResponseRedirect(reverse("vote:results", args=(question.id,))) | |
def results(request: HttpRequest, question_id: str): | |
question = get_object_or_404(Question, pk=question_id) | |
return render(request, "results.html", {"question": question}) | |
def create(request: HttpRequest): | |
if request.method == "POST": | |
question = Question( | |
question_text=request.POST["question_text"], | |
expire_date=request.POST["expire_date"], | |
) | |
question.save() | |
choices = request.POST.getlist("choices") | |
print(choices) | |
for choice_text in choices: | |
question.choice_set.create(choice_text=choice_text, votes=0) | |
return HttpResponseRedirect(reverse("vote:index") + "?created=True") | |
else: | |
return render(request, "create.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment