我正在尝试使用PasswordResetForm内置函数.
由于我想要自定义表单字段,我编写了自己的表单:
class FpasswordForm(PasswordResetForm):
email = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'autofocus': 'autofocus'}))
class Meta:
model = User
fields = ("email")
def clean_email(self):
email = self.cleaned_data['email']
if function_checkemaildomain(email) == False:
raise forms.ValidationError("Untrusted email domain")
elif function_checkemailstructure(email)==False:
raise forms.ValidationError("This is not an email adress.")
return email
Run Code Online (Sandbox Code Playgroud)
这是我在views.py中的观点
@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
def fpassword(request):
form = FpasswordForm(request.POST or None)
if form.is_valid():
email = form.cleaned_data["email"]
if function_checkemail(email):
form.save(from_email='blabla@blabla.com', email_template_name='registration/password_reset_email.html')
print "EMAIL SENT"
else:
print "UNKNOWN EMAIL ADRESS"
Run Code Online (Sandbox Code Playgroud)
我的电子邮件模板是:
{% autoescape off %}
You're receiving this e-mail because …
Run Code Online (Sandbox Code Playgroud) 以下代码无效
from django.utils.translation import gettext_lazy as _
stringtest=_("First string")
stringtest= stringtest + _(" Second string")
print stringtest
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
cannot concatenate 'str' and '__proxy__' objects
Run Code Online (Sandbox Code Playgroud)
是否真的不可能将"翻译"字符串附加到自己身上?
我试图从模型中的所有对象获取一个属性的内容列表.
现在,我这样做:
titles_list = []
for item in A.objects.all():
titles_list.append(item.title)
print titles_list
Run Code Online (Sandbox Code Playgroud)
是否有基于内存/时间经济的更有趣的解决方案呢?
我试图从模型A中获取一个随机对象
目前,它正在使用此代码:
random_idx = random.randint(0, A.objects.count() - 1)
random_object = A.objects.all()[random_idx]
Run Code Online (Sandbox Code Playgroud)
但我觉得这段代码更好:
random_object = A.objects.order_by('?')[0]
Run Code Online (Sandbox Code Playgroud)
哪一个是最好的?使用第一个代码删除对象可能出现问题?因为,例如,我可以有10个对象但是数字10作为id的对象不再存在?我在A.objects.all()[random_idx]中误解了什么吗?