這個效果,單位**是user model 的乙個外來鍵company通過修改form,然後在前端顯示
修改form
class authenticationform(forms.form):
"""base class for authenticating users. extend this to get a form that accepts
username/password logins.
"""username = forms.charfield(max_length=254)
password = forms.charfield(label=_("password"), widget=forms.passwordinput)
#自定義乙個company_form
company = forms.charfield(max_length=254)
error_messages =
def __init__(self, request=none, *args, **kwargs):
"""the 'request' parameter is set for custom auth use by subclasses.
the form data comes in via the standard 'data' kwarg.
"""self.request = request
self.user_cache = none
super(authenticationform, self).__init__(*args, **kwargs)
# set the label for the "username" field.
usermodel = get_user_model()
self.username_field = usermodel._meta.get_field(usermodel.username_field)
if self.fields['username'].label is none:
self.fields['username'].label = capfirst(self.username_field.verbose_name)
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
#有修改
company = self.cleaned_data.get('company')
if username and password and company:
self.user_cache = authenticate(username=username,
password=password,
company=company)
if self.user_cache is none:
raise forms.validationerror(
self.error_messages['invalid_login'],
code='invalid_login',
params=,
)else:
self.confirm_login_allowed(self.user_cache)
return self.cleaned_data
在admin form 當中找到這串**,主要是admin login 登入介面會引用,修改部分**然後在views當中修改login引用自定義的form然後跳轉到自定義的login頁面}}} login
} you are authenticated as }, but are not authorized to
access this page. would you like to login to a different account?
} 公司**
} }
使用者名稱} }
} }/*調節樣式*/
django admin 新增自定義鏈結方式
背景 最近做專案開發出現乙個需求,就是前端會發來使用者對某一項內容的報錯,報錯資訊中包含出錯內容的id,為了方便管理,需要實現點選這個id直接轉達相應內容的詳情頁面。效果展示 解決首先在django admin的列表中檢視資料id所代表的jbxdnvstu鏈結 使用瀏覽器的檢查元素功能檢視點選該id...
SpringSecurity 自定義表單登入
本篇主要講解 在springsecurity中 如何 自定義表單登入 springsecurity預設提供了乙個表單登入,但是實際專案裡肯定無法使用的,本篇就主要講解如何自定義表單登入 1.1 使用idea 先通過idea 建立乙個springboot專案 並且依賴springsecurity,we...
自定義 如何自定義協議
何為自定義協議,其實是相對標準協議來說的,這裡主要針對的是應用層協議 常見的標準的應用層協議如http ftp smtp等,如果我們在網路通訊的過程中不去使用這些標準協議,那就需要自定義協議,比如我們常用的rpc框架 dubbo,thrift 分布式快取 redis,memcached 等都是自定義...