Esempio n. 1
0
def cregiste(request):
    #GET 요청
    #ChoiceForm 객체 생성 및 HTML 전달
    if request.method == "GET":
        obj = ChoiceForm()
        return render(request, 'vote/cregieste.html', {'form': obj.as_table()})
    #POST 요청
    elif request.method == "POST":
        #사용자 입력기반으로 ChoiceForm 객체 생성
        obj = ChoiceForm(request.POST)
        #ChoiceForm객체를 기반으로 Choice객체 생성 및 데이터 베이스 저장
        #-> 값이 비어있는 변수가 없기 때문에(votes는 기본값 설정이 되어있음)
        c = obj.save()
        #다른 패이지로 이동(index나 detail로 이동)
        return HttpResponseRedirect('/vote/%s/' % c.q.id)
Esempio n. 2
0
def cregiste(request):
    #GET 요청
    if request.method == "GET":
    #ChoiceForm 객체 생성 및 HTML('vote.cregiste.html') 전달
        obj = ChoiceForm()
        return render(request, 'vote/cregiste.html',{'form': obj.as_table()})
    #POST 요청
    elif request.method == "POST":
        #사용자입력기반으로 ChoiceForm 객체 생성
        obj = ChoiceForm(request.POST)
        #ChoiceForm객체를 기반으로 Choice객체 생성 및 데이터베이스 저장
        c = obj.save()
        #->값이 비어있는 변수가 없기때문(votes는 기본값 설정이 되있음)
        #다른 페이지로 이동(index나 detail로 이동)
        #return HttpResponseRedirect('/vote/')#index페이지 url
        return HttpResponseRedirect('/vote/%s/'% c.q.id)#연동된 Question객체의 detail페이지의 url
Esempio n. 3
0
def c_update(request, c_id):
    c = get_object_or_404(Choice, id=c_id)
    if request.method == "POST":
        form = ChoiceForm(request.POST, instance=c)
        form.save()
        return HttpResponseRedirect(
            reverse('detail', args=(c.q.id,)))
    elif request.method =="GET":
        form = ChoiceForm(instance = c)
        result = form.as_table()
        #result = ChoiceForm(instance=c).as_table()
        #result : form태그 안에 들어갈 input태그 문자열
        #q_id (옵션): HTML코드에 detail페이지로 이동하기 위한
        #            Question객체의 id값 전달 
        #detail_url : 해당 수정페이지와 연관된 detail페이지의 주소전달
        detail_url = reverse('detail', args=(c.q.id,) )
        return render(request, "vote/c_update.html",
            {'result' : result, 'q_id' : c.q.id,
             'detail_url' : detail_url})
Esempio n. 4
0
def c_update(request, c_id):
    c = get_object_or_404(
        Choice, id=c_id)  #choice객체의 id가 c_id와 같은 값을 찾거나 없으면 404에러를 표시
    if request.method == "GET":
        form = ChoiceForm(
            instance=c)  #객체를 생성할 때 instance에 ChoiceForm에 넣어서 input처럼 만듬
        result = form.as_table()
        #위 두줄을 한줄로 만들기 result = ChoiceForm(instance=c).as_p()
        #result : form태그안에 들어갈 input태그 문자열
        #q_id (옵션) : html코드에 detail페이지로 이동하기 위한 Question객체의 id값 전달
        #detail_url : 해당 수정페이지와 연관된 detail 페이지의 주소 전달
        detail_url = reverse('detail', args=(c.q.id, ))
        return render(request, "vote/c_update.html", {
            'result': result,
            'q_id': c.q.id,
            'detail_url': detail_url
        })
    elif request.method == "POST":
        form = ChoiceForm(request.POST,
                          instance=c)  #instance에 수정할 대상을 넣음 instance=c
        form.save()
        return HttpResponseRedirect(reverse('detail', args=(c.q.id, )))
Esempio n. 5
0
def c_update(request, c_id):
    #수정할 Question 객체를 추출 하는 과정 필요
    #q_id: 어떤 객체를 수정할지 정보가 필요함
    #get_object_or_404 : 모델클래스의객체들중 id변수값을 이용해
    #객체 한개를 추출하는 함수. 단, 해당id값으로 된 객체가 없는 경우
    #사용자의 요청에 잘못있는 걸로 판단해 404에러 페이지를 전달한다.
    c = get_object_or_404(Choice, id=c_id)
    #Question 객체들을 대상으로 id값이 q_id값이 동일한 녀석을 q에 저장하겠다.
    #단, id값이 없으면 404에러값을 사용자에게 전달하겠다.

    #조건문 - POST, GET인지 구분 필요
    #GET으로 요청시 QuestionForm 객체 생성 - 수정할 객체를 바탕으로 생성

    if request.method == "GET":
        #form 클래스 객체 생성시 instance매개변수에 연동된 객체를 전달하면
        #해당 객체가 가진 값을 input태그에 채운 상태로 form 객체가 생성된다.
        form = ChoiceForm(instance=c)
        #인스탄스 매개변수 사용하는 이유는 - 수정할때 수정하려는 것의 제목과 내용이 뜨도록 하기 위해서 (네이버 글 수정하듯이)
        #as_table():각 입력공간과 설명을 <tr>과 <td>로 묶어주는 함수
        c_result = form.as_table()
        detail_url = reverse('detail', args=(c.q.id, ))
        return render(request, "vote/c_update.html", {
            'c_result': c_result,
            'id': c.q.id,
            'detail_url': detail_url
        })
        #누구한테 보낼것이냐(request), 무엇을 보낼것이냐("html"), 그 값을 보낼때 어떤 값으로 수정해서 보낼것이냐(딕셔너리형태)

    #POST 요청시 QuestionForm 객체생성 - 수정대상객체 + 사용자 입력으로 생성
    elif request.method == "POST":
        #수정대상객체와 사용자입력으로 form객체 생성시
        #기존의 객체 정보를 수정한 상태의 form 객체가 생성됨
        form = ChoiceForm(request.POST, instance=c)
        cc = form.save()  #사용자입력으로 수정한 결과를 데이터베이스에 반영
        idx = c.q.id
        print(c, cc)
        #detail view로 이동
        return HttpResponseRedirect(reverse('detail', args=(idx, )))