예제 #1
0
파일: views.py 프로젝트: kjin79/mysite7
def c_registe(request):
    if request.method == "GET":
        form = ChoiceForm()
        result = form.as_p()
        return render(request, "vote/c_registe.html", {'result': result})
    elif request.method == "POST":
        form = ChoiceForm(request.POST)
        new_c = form.save()
        idx = new_c.q.id
        return HttpResponseRedirect(reverse('detail', args=(idx, )))
예제 #2
0
def c_registe(request):
    #GET - 빈 ChoiceForm 객체 생성 및 html파일 전달
    if request.method == 'GET':
        form = ChoiceForm()

        asdf = form.as_p()

        return render(request, 'vote/c_registe.html', {'asdf': asdf})
    #POST - 사용자입력 기반의 ChoiceForm객체 생성 및 DB에 객체 저장 + detail 뷰로 이동
    if request.method == 'POST':
        form = ChoiceForm(request.POST)

        new_c = form.save()

        return HttpResponseRedirect(reverse('detail', args=(new_c.q.id, )))
예제 #3
0
def c_registe(request):
    #사용자 요청이 GET인지 POST인지 구분
    if request.method == "GET":
        #GET으로 요청했을때는 HTML로 제공
        #ChoiceForm 객체를 생성
        #ChoiceForm 객체를 생성할 때 입력값이 없는 형태로 생성하면
        #Input 태그에 아무런 값도 입력되있지 않은 상태의 객체가 생성됨
        form = ChoiceForm()  #ChoiceFrom 객체 하나 생성

        #객체를 바탕으로 HTML 코드로 변환

        #as_p(),as_table,as_ul함수 : Form 객체에 입력할 수 있는
        #공간들을 <input>으로 변환하면서 <p>,<tr>,<td><li<태그로
        #감싸누는 기능이 있는 함수

        c_rendered = form.as_p()
        print(c_rendered)
        #변환값을 HTML 파일에 전달
        return render(request, "vote/c_registe.html",
                      {'c_rendered': c_rendered})

    elif request.method == "POST":
        #POST로 요청했을때는 다른 주소를 제공
        #QuestionForm 객체를 생성 - 사용자 입력 바탕으로 생성
        form = ChoiceForm(request.POST)
        #사용자가 입력한 값 기반으로 데이터 생성?

        #QuestionForm 객체와 연동된 Question 객체를 생성 및 저장
        #form.save(commit=False)
        # : 연동된 모델클래스의 객체로 변환만 하는 함수
        # 데이터 베이스에 저장은 하지 않고 객체로 변경만?
        #form.save():연동된 모델클래스의 객체를 생성 및 데이터 베이스에 저장
        new_c = form.save()
        idx = new_c.q.id
        print(new_c)
        #새로운 객체가 형성되었다.
        #생성된 Question객체의 id값으로 detail뷰의 링크를 전달
        return HttpResponseRedirect(reverse('detail', args=(idx, )))