如何实现从视图到表单的所有业务逻辑?

时间:2019-03-11 06:13:30

标签: python django

我正在开发一个Web应用程序,我想将视图中的逻辑移到表单中。在视图中,我只想保留验证检查。那就是:

示例

def myformview(request):
    if request.POST:
        form = MForm(request.POST)
        if form.is_valid():
            form.save()
            redirect("to-some-view")

    render(request, "template_name.html", {
        'form': form
    })

这是我的 views.py

def login_user(request):
    if request.method == 'POST':
        login_form = Login(request.POST)
        if login_form.is_valid():
            email = login_form.cleaned_data['email']
            password = login_form.cleaned_data.get('password')
            user = authenticate(email=email, password=password)
            if user in User.objects.all():
                login(request, user)
                return HttpResponseRedirect(reverse('dashboard'))
            else:
                return render(request, 'todoapp/waiting_2.html')

    return render(request, 'registration/login.html', {'form': Login()})

这是我的 forms.py

class Login(forms.Form):
    email = forms.EmailField(max_length=250)
    password = forms.CharField(widget=forms.PasswordInput)

目前一切正常。我只想将视图中的逻辑移到表单。就是这样。

编辑: 我更改了forms.py和views.py,但仍然无法正常工作,并且出现了Page Not Found 404错误。

这是我的新 forms.py

class Login(forms.Form):
    email = forms.EmailField(max_length=250)
    password = forms.CharField(widget=forms.PasswordInput)

    def login_user(self):
        email = self.cleaned_data['email']
        password = self.cleaned_data.get('password')
        user = authenticate(email=email, password=password)
        if user in User.objects.all():
            login(self, user)
        else:
            return render(self, 'todoapp/waiting_2.html')

这是我的新 views.py:

def login_user(request):
    if request.method == 'POST':
        login_form = Login(request.POST)
        if login_form.is_valid():
            return HttpResponseRedirect(reverse('dashboard'))
        else:
            return render(request, 'todoapp/waiting_2.html')
    return render(request, 'registration/login.html', {'form': Login()})

这是我的 urls.py

from django.conf.urls import url
from .import views

urlpatterns = [
    url(r'^index/', views.index, name='index'),
    url(r'^register/', views.register, name='register'),
    url(r'^login/', views.login_user, name='login_user'),
    url(r'^dashboard/', views.dashboard, name='dashboard'),
    url(r'^logout_user/', views.logout_user, name='logout_user'),
    url(r'^auth_users/', views.auth_users, name='auth_users'),
    # url(r'^authorize_final', views.authorize_final, name='authorize_final'),
    url(r'^assigntask', views.assigntask, name='assigntask'),
    # url(r'^assign', views.assign, name='assign'),
    url(r'^task_list', views.view_task_list, name='view_task_list'),
    # url(r'^delete_task', views.delete_task, name='delete_task'),
]

我遇到的错误是:

AttributeError at /login/

'Login' object has no attribute 'session'

跟踪:

File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/gblp250/PycharmProjects/assignment/todoapp/views.py" in login_user
  48.             login_form.login_user(request)

File "/home/gblp250/PycharmProjects/assignment/todoapp/forms.py" in login_user
  27.             login(self, request, user)

File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
  126.     if SESSION_KEY in request.session:

Exception Type: AttributeError at /login/
Exception Value: 'Login' object has no attribute 'session'

1 个答案:

答案 0 :(得分:0)

为此,您可以在表单中覆盖is_valid方法(请参阅this link)并将逻辑放在此处,但是,更合适的方法是使用基于类的视图{{1 }},然后对其进行操作。

请参阅this link,以了解FormView通用的基于类的方法。一旦了解了使用方法,这将大大简化该过程。

编辑:

总体上看起来不错,但逻辑实现仍然错误

FormView

这是我的新views.py ::

class Login(forms.Form):
    email = forms.EmailField(max_length=250)
    password = forms.CharField(widget=forms.PasswordInput)

    def login_user(self, request):
        email = self.cleaned_data['email']
        password = self.cleaned_data.get('password')
        user = authenticate(request, email=email, password=password)
        if user:
            login(request, user) // self should have been replaced by request
        else:
            return HttpResponseRedirect(reverse('view_task_list'))//You can redirect to any url

这是我的urls.py:

def login_user(request):
    if request.method == 'POST':
        login_form = Login(request.POST)
        if login_form.is_valid():
            login_form.login_user(request) //just call the method created in the form
    else:
        login_form = Login() //this is for GET REQUEST
    return render(request, 'registration/login.html', {'form': login_form}) //you should return instance of the form

此外,由于使用默认的from django.conf.urls import url from .import views urlpatterns = [ url(r'^index/', views.index, name='index'), url(r'^register/', views.register, name='register'), url(r'^login/', views.login_user, name='login_user'), url(r'^dashboard/', views.dashboard, name='dashboard'), url(r'^logout_user/', views.logout_user, name='logout_user'), url(r'^auth_users/', views.auth_users, name='auth_users'), # url(r'^authorize_final', views.authorize_final, name='authorize_final'), url(r'^assigntask', views.assigntask, name='assigntask'), # url(r'^assign', views.assign, name='assign'), url(r'^task_list', views.view_task_list, name='view_task_list'), # url(r'^delete_task', views.delete_task, name='delete_task'), ] 方法,因此必须在settings.py中设置登录和注销重定向URL

login()