Django根据单选按钮的选择呈现视图

时间:2018-04-03 14:05:21

标签: django django-forms django-views

我有一个django项目,我必须根据在单选按钮中选择的选项来呈现视图,即如果学生被选中则必须转到一个页面,如果教师被选中,它应该转到另一个页面 forms.py

DISPLAY_CHOICES = (
(1, "Student"),
(2, "Faculty")
)        

class Wru(forms.Form):
display_type = forms.ChoiceField(widget=forms.RadioSelect, 
choices=DISPLAY_CHOICES)

views.py

def index(request):
    form=Wru(request.POST)
    if request.method == 'POST':
        if form.is_valid():
          if form.fields['display_type'].choices[1]:
            return home(request)
          else:
            return simple_upload(request)
    return render(request, 'acads/index.html', {'form': form})

1 个答案:

答案 0 :(得分:0)

您可能想要使用重定向功能。

https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#redirect

def index(request):
    form=Wru(request.POST)
    if request.method == 'POST':
        if form.is_valid():
          if form.fields['display_type'].choices[1]:
             return redirect('/url/of/homepage')
          else:
             return redirect('url/of/simple_upload/)
    return render(request, 'acads/index.html', {'form': form})