如何在django中的不同视图中使用一个post方法两次

时间:2014-02-12 14:17:05

标签: python django python-2.7 django-forms django-views

我在views.py中有一个post方法模板(即foo_form.html)和两个函数(foo1,foo2),我想使用post值。

我的foo_form.html就在这里

<html>{% load i18n %}
  <head>
  <form method="POST" enctype="multipart/form-data" >
  {% csrf_token %}
    <input type="text" name= "month"> </input>
    <input type="text" name= "year"> </input>
    <input type="submit" value="view">
   </form>

在foo1功能中,我需要做一些任务以及验证月份和时间。年份并重定向到security_check页面,我需要检查他是否是合适的人才能看到该页面。

现在,如果他通过了检查任务,那么重定向到foo2功能。在foo2函数中我必须使用month&amp;一年&GT;我的forms.py就像这样

class month_Form(forms.Form):
  Month = forms.IntegerField()
  Year = forms.IntegerField()

我的views.py就像这样

def foo1(request):
  if request.method == 'POST':
    month_form = month_Form(request.POST)
    if pay_form.is_valid():
        # do some task
        return HttpResponseRedirect('/security_check/')
  return render(request, 'foo_form.html', {})

def foo2(request):
  month_form1 = month_Form(request.POST) # I cann't get whether request.method == 'POST': should be there or not?
  month_no = request.POST.get('Month',False)
  # unable to retrieve month_no 
  # and in debug method is showing "GET"

我的/ security_check / function就像这样

def security_check(request):
  if request.method == 'POST':
    month_form = month_Form(request.POST)
    if pay_form.is_valid():
      # validated weather he has the permission to go to foo2
      #redirect to foo2
  #render to security_check_form.html(this is a security que form html)

我正在使用Django 1.6。提前致谢

1 个答案:

答案 0 :(得分:0)

哦,我是从Django doc.

得到的

我可以创建security_check功能视图,我可以自定义我自己的表单(如@login required())

def foo1(request):
   if request.method==POST:
     #retrieve value in POST method 
     #call foo2
     return render (request,destination.html{variables})
   return render(request, 'foo_form.html', {}) 

@user_passes_test(security_check[, login_url=None])
def foo2(postdatas):
  #do some task
  # return tapule on the basis of requirement