0
context_processor.py:語境處理器forms.py
def max_min(request):
"""
some code. request required, because I need
calculate some information about each user
"""
return {'max': max, 'min':max}
forms.py:
class MaxMin(forms.Form):
my_field = forms.IntegerField(
min_value=min, max_value=max, required=True,
widget=forms.NumberInput(attrs={'class':'form-control',
'required': 'true'}))
所以,我有代碼context_processor.py(它使用上所有頁面的網站),我想用在我的forms.py。但我不知道,如何導入和使用它。 MAX_MIN需求的要求,但在forms.py我沒有它。
是的,我知道,我可以用{{min}}
和HTML表單{{max}}
,沒有forms.py,但後來我需要在代碼檢查,什麼價值觀我通過POST了。
謝謝。
UPDATE:
class MAxMin(forms.Form):
def __init__(self, req, *args, **kwargs):
super(MAxMin, self).__init__(*args, **kwargs)
self.request = req
def mymethod(self):
return max_min(self.request)
my_field = forms.IntegerField(
min_value=mymethod(), required=True,
widget=forms.NumberInput(attrs={'class':'form-control',
'required': 'true'}))
小心,通過重寫形式'__init__'喜歡你已經禁用的正常參數(數據,初始,等等)。你應該總是接受'*指定參數時,** kwargs'並將它們傳遞到'super'電話。 –
是的,好點。我編輯了答案。 –
@AlexParakhnevich我用你的代碼,我有一個錯誤:'名「MAX_MIN」不defined',然後我加'從project.context_processor進口max_min',我有新的錯誤:'名「自我」是不是defined' – tim