本文整理汇总了Python中forms.LoginForm._errors["username"]方法的典型用法代码示例。如果您正苦于以下问题:Python LoginForm._errors["username"]方法的具体用法?Python LoginForm._errors["username"]怎么用?Python LoginForm._errors["username"]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.LoginForm
的用法示例。
在下文中一共展示了LoginForm._errors["username"]方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import _errors["username"] [as 别名]
def register(request):
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data["username"]
password = form.cleaned_data["password"]
try:
u = User.objects.get(email=email)
u = authenticate(username=u.username, password=password)
if u is not None:
login(request, u)
else:
form._errors["username"] = ErrorList(["invalid username/password"])
except:
u = User.objects.create_user(email, email, password)
u = authenticate(username=email, password=password)
login(request, u)
return HttpResponseRedirect("/")
else:
return HttpResponseRedirect("/")
示例2: user_login
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import _errors["username"] [as 别名]
def user_login(request):
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
print username, password
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
status = "success"
else:
# Return a 'disabled account' error message
status = "failure"
else:
# Return an 'invalid login' error message.
status = "failure"
print request.user.is_authenticated()
"""
url = HttpRequest.build_absolute_uri(request, '/polls/api/login/')
print(url)
values = {
'username': username,
'password': password
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
res = response.read()
res_json = json.loads(res)
"""
if status == "success":
return HttpResponseRedirect("/")
else:
form._errors["username"] = ErrorList(["invalid username/password"])
else:
form = LoginForm()
return render(request, "polls/v1_base.html", {"form": form})
示例3: home
# 需要导入模块: from forms import LoginForm [as 别名]
# 或者: from forms.LoginForm import _errors["username"] [as 别名]
def home(request):
if request.user.is_authenticated():
"""
answered = [vote.question.id for vote in Vote.objects.filter(user=request.user)]
ignored = [ig.question.id for ig in Ignored.objects.filter(user=request.user)]
unanswered = Question.objects.exclude(id__in=answered).exclude(id__in=ignored)
new_questions = random.sample(unanswered,min(len(unanswered),10))
"""
new_questions = Feed.user_feed(request.user)
for q in new_questions:
try:
suser = UserSocialAuth.objects.get(user_id=q.user.id)
image_url = "http://graph.facebook.com/{0}/picture".format(suser.uid)
except:
image_url = "/static/polls/images/user.png"
q.user.image_url = image_url
context = {"nav": "home", "questions": new_questions}
return render(request, "polls/v1_feed.html", context)
else:
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
print username, password
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
status = "success"
else:
# Return a 'disabled account' error message
status = "failure"
else:
# Return an 'invalid login' error message.
status = "failure"
print request.user.is_authenticated()
"""
url = HttpRequest.build_absolute_uri(request, '/polls/api/login/')
print(url)
values = {
'username': username,
'password': password
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
res = response.read()
res_json = json.loads(res)
"""
if status == "success":
return HttpResponseRedirect("/")
else:
form._errors["username"] = ErrorList(["invalid username/password"])
else:
form = LoginForm()
# recently finished polls
questions = Question.objects.all().order_by("-id")[:10]
for q in questions:
choices = []
for choice in q.choice_set.all():
choices.append({"cid": choice.id, "ctext": choice.choice_text, "cvotes": choice.votes})
choices = sorted(choices, key=lambda k: k["cvotes"], reverse=True)
q.choices = json.dumps(choices)
context = {"form": form, "nav": "home", "questions": questions}
return render(request, "polls/v1_login.html", context)