Beispiel #1
0
def c_update(request, c_id):
    c = get_object_or_404(Choice, id=c_id)
    #GET - 추출한 Choice 객체 기반의 ChoiceForm 객체 생성 및 html 전달
    if request.method == 'GET':
        #form = ChoiceForm(instance=c)
        #qwer = form.as_ul()
        qwer = ChoiceForm(instance=c).as_ul()
        detail_url = reverse('detail', args=(c.q.id, ))

        return render(
            request,
            'vote/c_update.html',
            #qwer : form태그 안에 들어갈 input태그 문자열
            #q_id(옵션) : HTML코드에 detail페이지로 이동하기 위한 Question객체의 id값 전달
            #detail_url : 해당 수정페이지와 연관된 detail페이지 주소 전달
            {
                'qwer': qwer,
                'q_id': c.q.id,
                'detail_url': detail_url
            })
    #POST - 추출한 Choice 객체 + 사용자 입력 기반의 ChoiceForm객체 생성 및 수정사항을 DB에 반영 + detail 뷰로 이동
    if request.method == 'POST':
        form = ChoiceForm(request.POST, instance=c)
        form.save()
        return HttpResponseRedirect(reverse('detail', args=(c.q.id, )))
Beispiel #2
0
def cregister(request):
    if request.method =='GET':
        f = ChoiceForm()
        return render(request,'vote/cform.html',{'f':f})
    elif request.method =='POST':    
        f = ChoiceForm(request.POST)
        c = f.save()
        return HttpResponseRedirect(reverse('vote:detail', args=(c.q.id,)))
Beispiel #3
0
def cregister(request):
    if request.method == 'GET':
        f = ChoiceForm()
        return render(request, 'vote/cform.html', {'f': f})
    elif request.method == 'POST':
        f = ChoiceForm(request.POST)
        c = f.save()
        #index 뷰로 이동
        #return HttpResponseRedirect(reverse('vote:index'))
        #삭제한 Choice객체가 연결된 Question객체의 detail로 이동
        return HttpResponseRedirect(reverse('vote:detail', args=(c.q.id, )))
Beispiel #4
0
def cupdate(request, c_id):
    c = get_object_or_404(Choice, id=c_id)
    if request.method == "GET":
        obj = ChoiceForm(instance=c)
        return render(request, 'vote/cupdate.html', {'obj': obj})
    elif request.method == "POST":
        obj = ChoiceForm(request.POST, instance=c)
        obj.save()
        return HttpResponseRedirect('/vote/%s/' % c.q.id)
Beispiel #5
0
def cupdate(request,c_id):
     c = get_object_or_404(Choice,id=c_id)
     if request.method =='GET':
        f = ChoiceForm(instance = c)
        return render(request, 'vote/cform.html',{'f':f})
     elif request.method =='POST':    
        f = ChoiceForm(request.POST, instance = c)
        f.save()
        return HttpResponseRedirect(reverse('vote:detail', args=(c.q.id,)))
Beispiel #6
0
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, )))
Beispiel #7
0
def cupdate(request, c_id):
    c = get_object_or_404(Choice, id=c_id)
    if request.method == "GET":
        f = ChoiceForm(instance=c)
        return render(request, 'vote/cform.html', {'f': f})
    elif request.method == "POST":
        f = ChoiceForm(request.POST, instance=c)
        f.save()
        #index 뷰로 이동
        #return HttpResponseRedirect(reverse('vote:index'))
        #삭제한 Choice객체가 연결된 Question객체의 detail로 이동
        return HttpResponseRedirect(reverse('vote:detail', args=(c.q.id, )))
Beispiel #8
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, )))
Beispiel #9
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)
Beispiel #10
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})
Beispiel #11
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
Beispiel #12
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, )))
Beispiel #13
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, )))
Beispiel #14
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, )))