覆盖django-allauth默认表单 [英] override django-allauth default forms

查看:262
本文介绍了覆盖django-allauth默认表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖django-allauth的默认形式,以便在不更改allauth表单的初始代码的情况下更改\add默认的widget属性,类变量。
我该怎么做?



我的工作区:

1)安装django-allauth,作为一个包通过pip;

2)配置设置,根据github项目的建议;

3)本地创建的模板登录,注册;

4)创建Forms.py,它定义了一个子类,从allauth.account导入形式django-allauth

 
SigninForm

class mySigninForm(SigninForm):
username = ... //覆盖变量

这是正确的吗?



谢谢!

解决方案

如果您想使用allauth表单提供的方法或功能,则覆盖表单类像你现在是你最好的选择。

  from allauth.account.forms import LoginForm 
from django import forms
from myapp.forms import widgets


class MyLoginForm(LoginForm):

//覆盖属性
existing_field = forms.CharField(widget = widgets.CustomWidget())

//添加自定义属性
new_field = forms.CharField(widget = widgets.CustomWidget())
/ pre>

如果您想要一个完全自定义的表单,可以使用自定义django表单,然后在视图中使用它。例如:



表单



 从django导入表单
从myapp.forms导入小部件

类MyLoginForm(forms.Form):
//添加属性和方法
some_field = forms.CharField(widget = widgets.CustomWidget() )



视图



 code>从django.views.generic.edit导入FormView 
from myapp.forms import MyLoginForm

LoginUser(FormView):
form = MyLoginForm


I want to override the default forms of django-allauth in order to change\add default widget attributes, class variables without changing the initial code of allauth form. How can I do that?

My workspace:
1) installed django-allauth, as a package through pip;
2) configured settings, according to the recommendations of the project on github;
3) locally created templates signin, signup;
4) created forms.py, which defines a child class forms django-allauth

from allauth.account import SigninForm

class mySigninForm (SigninForm):
     username = ...//override variable

Is this right way?

Thanks!

解决方案

If you'd like to use the methods or functionalities provided by the allauth forms then overriding the form classes like you currently are is your best bet.

from allauth.account.forms import LoginForm
from django import forms
from myapp.forms import widgets


class MyLoginForm(LoginForm):

    // Override attributes
    existing_field = forms.CharField(widget=widgets.CustomWidget())

    // Add Custom Attributes
    new_field = forms.CharField(widget=widgets.CustomWidget())

If you want a completely custom form, you can use a custom django form and then use it in the view. For Example:

forms

from django import forms
from myapp.forms import widgets

class MyLoginForm(forms.Form):
    // Add atributes and methods
    some_field = forms.CharField(widget=widgets.CustomWidget())

views

from django.views.generic.edit import FormView
from myapp.forms import MyLoginForm

LoginUser(FormView):
    form = MyLoginForm

这篇关于覆盖django-allauth默认表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆