基于Django类的视图登录

时间:2017-07-24 16:50:55

标签: python django

我正在尝试将function based view转换为class based view中的Django。视图的功能是显示主页以及登录/注销表单/按钮。 enter image description here

基本上,如果用户已登录,则注销按钮将显示在Account下方,如果用户已注销,则会显示login表单。所以它适用于我的基于函数的视图:

views.py

def _get_login_form(request):
    form = UserLoginForm(request.POST or None)
    if request.POST and form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        if user:
            login(request, user)

    return form

@csrf_protect
def index(request):
    if request.user.is_authenticated():
        form = forms.Form(request.POST or None)
        if 'logout-button' in request.POST and form.is_valid():
            logout(request)
            form = _get_login_form(request)
    else:
        form = _get_login_form(request)

    info = {
        'company_name' : 'Purpose VC',
        'form': form,
        'lorem': '''Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
 Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'''
    }
    return render(request, 'main/index.html', info)

但我遇到了Class based View的问题。这就是我到目前为止所做的:

class IndexView(TemplateView):
    template_name = 'main/index.html'

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['company_name'] = 'Purpose VC'
        context['lorem'] = '''Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
 Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'''
        context['form'] = kwargs['form']
        return context

    def get(self, request):
        form = UserLoginForm(request.POST or None)
        return self.get_context_data(self, form)

    def post(self, request):
        if request.user.is_authenticated():
            if 'logout-button' in request.POST and form.is_valid():
                logout(request)
                form = self._get_login_form(request)
        else:
            form = self._get_login_form(request)

        return self.get_context_data(self, form)

    def _get_login_form(self, request):
        form = UserLoginForm(request.POST or None)
        if request.POST and form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user:
                login(request, user)

        return form

根据用户是否经过身份验证,我无法传递相应的表单。我得到的具体错误是context['form'] = kwargs['form'] KeyError: 'form' 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

试试这个

context['form'] = self.kwargs.get('form')