- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Following is view.py which includes
signup/loginview
view.py
from accounts.forms import SettingsForm
from accounts.mixins import LoginRequiredMixin
from accounts.models import SignupCode, EmailAddress, EmailConfirmation, Account, AccountDeletion
from accounts.utils import default_redirect,user_display
class SignupView(FormView):
template_name = "account/signup.html"
template_name_ajax = "account/ajax/signup.html"
template_name_email_confirmation_sent = "account/email_confirmation_sent.html"
template_name_email_confirmation_sent_ajax = "account/ajax/email_confirmation_sent.html"
template_name_signup_closed = "account/signup_closed.html"
template_name_signup_closed_ajax = "account/ajax/signup_closed.html"
form_class = SignupForm
form_kwargs = {}
redirect_field_name = "next"
messages = {
"email_confirmation_sent": {
"level": messages.INFO,
"text": _("Confirmation email sent to %(email)s.")
},
"invalid_signup_code": {
"level": messages.WARNING,
"text": _("The code %(code)s is invalid.")
}
}
def __init__(self, *args, **kwargs):
self.created_user = None
kwargs["signup_code"] = None
super(SignupView, self).__init__(*args, **kwargs)
def get(self, *args, **kwargs):
if self.request.user.is_authenticated():
return redirect(default_redirect(self.request, AccountAppConf.LOGIN_REDIRECT_URL))
if not self.is_open():
return self.closed()
return super(SignupView, self).get(*args, **kwargs)
def post(self, *args, **kwargs):
if not self.is_open():
return self.closed()
return super(SignupView, self).post(*args, **kwargs)
def get_initial(self):
initial = super(SignupView, self).get_initial()
if self.signup_code:
initial["code"] = self.signup_code.code
if self.signup_code.email:
initial["email"] = self.signup_code.email
return initial
def get_template_names(self):
if self.request.is_ajax():
return [self.template_name_ajax]
else:
return [self.template_name]
def get_context_data(self, **kwargs):
ctx = kwargs
redirect_field_name = self.get_redirect_field_name()
ctx.update({
"redirect_field_name": redirect_field_name,
"redirect_field_value": self.request.REQUEST.get(redirect_field_name),
})
return ctx
def get_form_kwargs(self):
kwargs = super(SignupView, self).get_form_kwargs()
kwargs.update(self.form_kwargs)
return kwargs
def form_invalid(self, form):
signals.user_sign_up_attempt.send(
sender=SignupForm,
username=form.data.get("username"),
email=form.data.get("email"),
result=form.is_valid()
)
return super(SignupView, self).form_invalid(form)
def form_valid(self, form):
self.created_user = self.create_user(form, commit=False)
# prevent User post_save signal from creating an Account instance
# we want to handle that ourself.
self.created_user._disable_account_creation = True
self.created_user.save()
email_address = self.create_email_address(form)
if AccountAppConf.EMAIL_CONFIRMATION_REQUIRED and not email_address.verified:
self.created_user.is_active = False
self.created_user.save()
self.create_account(form)
self.after_signup(form)
if AccountAppConf.EMAIL_CONFIRMATION_EMAIL and not email_address.verified:
email_address.send_confirmation()
if AccountAppConf.EMAIL_CONFIRMATION_REQUIRED and not email_address.verified:
return self.email_confirmation_required_response()
else:
show_message = [
AccountAppConf.EMAIL_CONFIRMATION_EMAIL,
self.messages.get("email_confirmation_sent"),
not email_address.verified
]
if all(show_message):
messages.add_message(
self.request,
self.messages["email_confirmation_sent"]["level"],
self.messages["email_confirmation_sent"]["text"] % {
"email": form.cleaned_data["email"]
}
)
self.login_user()
return redirect(self.get_success_url())
def get_success_url(self, fallback_url=None, **kwargs):
if fallback_url is None:
fallback_url = AccountAppConf.SIGNUP_REDIRECT_URL
kwargs.setdefault("redirect_field_name", self.get_redirect_field_name())
return default_redirect(self.request, fallback_url, **kwargs)
def get_redirect_field_name(self):
return self.redirect_field_name
def create_user(self, form, commit=True, **kwargs):
user = User(**kwargs)
username = form.cleaned_data.get("username")
if username is None:
username = self.generate_username(form)
user.username = username
user.email = form.cleaned_data["email"].strip()
password = form.cleaned_data.get("password")
if password:
user.set_password(password)
else:
user.set_unusable_password()
if commit:
user.save()
return user
def create_account(self, form):
return Account.create(request=self.request, user=self.created_user, create_email=False)
def generate_username(self, form):
raise NotImplementedError("Unable to generate username by default. "
"Override SignupView.generate_username in a subclass.")
def create_email_address(self, form, **kwargs):
kwargs.setdefault("primary", True)
kwargs.setdefault("verified", False)
if self.signup_code:
self.signup_code.use(self.created_user)
kwargs["verified"] = self.signup_code.email and self.created_user.email == self.signup_code.email
return EmailAddress.objects.add_email(self.created_user, self.created_user.email, **kwargs)
def after_signup(self, form):
signals.user_signed_up.send(sender=SignupForm, user=self.created_user, form=form)
def login_user(self):
# set backend on User object to bypass needing to call auth.authenticate
self.created_user.backend = "django.contrib.auth.backends.ModelBackend"
auth.login(self.request, self.created_user)
self.request.session.set_expiry(0)
def is_open(self):
code = self.request.REQUEST.get("code")
if code:
try:
self.signup_code = SignupCode.check(code)
except SignupCode.InvalidCode:
if self.messages.get("invalid_signup_code"):
messages.add_message(
self.request,
self.messages["invalid_signup_code"]["level"],
self.messages["invalid_signup_code"]["text"] % {
"code": code
}
)
return AccountAppConf.OPEN_SIGNUP
else:
return True
else:
return AccountAppConf.OPEN_SIGNUP
def email_confirmation_required_response(self):
if self.request.is_ajax():
template_name = self.template_name_email_confirmation_sent_ajax
else:
template_name = self.template_name_email_confirmation_sent
response_kwargs = {
"request": self.request,
"template": template_name,
"context": {
"email": self.created_user.email,
"success_url": self.get_success_url(),
}
}
return self.response_class(**response_kwargs)
def closed(self):
if self.request.is_ajax():
template_name = self.template_name_signup_closed_ajax
else:
template_name = self.template_name_signup_closed
response_kwargs = {
"request": self.request,
"template": template_name,
}
return self.response_class(**response_kwargs)
class LoginView(FormView):
template_name = "account/login.html"
template_name_ajax = "account/ajax/login.html"
form_class = LoginUsernameForm
form_kwargs = {}
redirect_field_name = "next"
def get(self, *args, **kwargs):
if self.request.user.is_authenticated():
return redirect(self.get_success_url())
return super(LoginView, self).get(*args, **kwargs)
def get_template_names(self):
if self.request.is_ajax():
return [self.template_name_ajax]
else:
return [self.template_name]
def get_context_data(self, **kwargs):
ctx = kwargs
redirect_field_name = self.get_redirect_field_name()
ctx.update({
"redirect_field_name": redirect_field_name,
"redirect_field_value": self.request.REQUEST.get(redirect_field_name),
})
return ctx
def get_form_kwargs(self):
kwargs = super(LoginView, self).get_form_kwargs()
kwargs.update(self.form_kwargs)
return kwargs
def form_invalid(self, form):
signals.user_login_attempt.send(
sender=LoginView,
username=form.data.get(form.identifier_field),
result=form.is_valid()
)
return super(LoginView, self).form_invalid(form)
def form_valid(self, form):
self.login_user(form)
self.after_login(form)
return redirect(self.get_success_url())
def after_login(self, form):
signals.user_logged_in.send(sender=LoginView, user=form.user, form=form)
def get_success_url(self, fallback_url=None, **kwargs):
if fallback_url is None:
fallback_url = AccountAppConf.LOGIN_REDIRECT_URL
kwargs.setdefault("redirect_field_name", self.get_redirect_field_name())
return default_redirect(self.request, fallback_url, **kwargs)
def get_redirect_field_name(self):
return self.redirect_field_name
def login_user(self, form):
auth.login(self.request, form.user)
expiry = AccountAppConf.REMEMBER_ME_EXPIRY if form.cleaned_data.get("remember") else 0
self.request.session.set_expiry(expiry)
Following is
account/url.py
given by Pinax
account/url.py
from accounts.views import SignupView, LoginView, LogoutView, DeleteView
from accounts.views import ConfirmEmailView
from accounts.views import ChangePasswordView, PasswordResetView, PasswordResetTokenView
from accounts.views import SettingsView,Signup_login
urlpatterns = patterns("",
url(r"^signup/$", SignupView.as_view(), name="account_signup"),
url(r"^login/$", LoginView.as_view(), name="account_login"),
url(r"^logout/$", LogoutView.as_view(), name="account_logout"),
url(r"^confirm_email/(?P<key>\w+)/$", ConfirmEmailView.as_view(), name="account_confirm_email"),
url(r"^password/$", ChangePasswordView.as_view(), name="account_password"),
url(r"^password/reset/$", PasswordResetView.as_view(), name="account_password_reset"),
url(r"^password/reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$", PasswordResetTokenView.as_view(), name="account_password_reset_token"),
url(r"^settings/$", SettingsView.as_view(), name="account_settings"),
url(r"^delete/$", DeleteView.as_view(), name="account_delete"),
url(r"^signup.login/$", Signup_login.as_view(), name="Signup_login"),
)
And following is my project's
app/url.py
url.py
urlpatterns = patterns("",
url(r"^$",SignupView.as_view(template_name="homepage.html"), name="account_signup"),
url(r"^$", LoginView.as_view(template_name="homepage.html"), name="account_login"),
url(r"^admin/", include(admin.site.urls)),
url(r"^account/", include("accounts.urls")),
)
我正在尝试做的事情:调用 homepage.html
上的两个 View 。它在单个 html 中包含两种形式。
最佳答案
不要这样做。试着全神贯注于浏览器和网络服务器之间发生的事情。 URL 标识服务器将返回给客户端的内容。调用 2 个“ View ”违反了该原则。
您真正想要做的是创建一个新的 URL,或者修改模板以将两个表单放在同一页面上。我可能会重命名 SignupOrLoginView 并在该单一 View 中完成注册工作。
这在功能 View 中会更容易理解,而不是基于类的 View ,后者似乎将您限制在单一形式。
关于python - 如何在pinax中调用一个url的多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18765224/
假设我拥有域 mydomain.com,并且我在服务器上有一个 Web 应用程序,网址为 http://99.99.99.99:1234/MyApplication/startpage.somethi
我正在尝试通过以下方式更新已解析的 URL: u, _ := url.Parse(s) if u.Scheme == "" { u.Scheme = "https" } if u.Path =
如何将 www.somesite.com/api(.*) 映射到 www.somesite.com/$1:9000? (我需要将/api 映射到运行 @ 端口 9000 的 Play 框架应用程序)
我有一个资源结构,如航类 > 座位 > 预订,所以预订属于某个航类的某个座位: http://example.com/jdf_3prGPS4/1/jMBDy46PbNc
我想知道以下网址是否有效。 路径中的点,在主机之后: http://www.example.com/v.b.w..com 主机中的点,作为子域的一部分: http://v.b.w..co.manufa
我有两个域 - crmpicco.co.uk 和 ayrshireminis.com - 如果我浏览到: www.crmpicco.co.uk/mini/new我希望能够重定向到 www.ayrshi
我正在尝试使用 URL 重写和应用程序请求路由来重写到外部 URL。我设置了以下规则: 在规则中,“patternToMatch”是我试
我已经安装了带有 SharePoint 和 Url Rewrite 模块的 IIS 7.0。 是以下句子还是我配置错误才能看到这个结果? Url Redirect 可以将 url 重定向到任何内部(在
我想知道,为了获得良好的 SEO,您必须在 URL 中使用自然语言。您知道字符中单词或短语的最大大小吗?例如: www.me.com/this-is-a-really-long-url.htm 我问这
有人知道在 SEO 友好 URL 中使用逗号有什么问题吗?我正在使用一些在其 SEO 友好 URL 中使用大量逗号的软件;但我 100% 肯定我见过一些程序/平台无法正确识别 URL 并在第一个逗号后
我有一个网站,我正在为所有链接使用干净的 URL。我想知道对于简短的基本 URL 与较长的描述性 URL 有何看法。 例如,如果我的网站是关于 Georgia Bulldog 足球新闻的,那么哪个网站
我正在编写一个类似于 tinyurl 的 URL 缩短器,我想知道如何跟踪已经使用我的服务缩短的 URL?例如,tinyurl 为相同的长 URL 生成相同的小 URL,而不管是谁创建的。如
我是 magento 的新手。我正在开发一个模块。为此,我有一些要显示链接的 css 和 js 文件。我目前有类似 的链接 getSkinUrl('module_tryouts/css/jquery.
我想基于 HTTP_URL 重写 URL 以重定向到不同的端口,同时保留其余的 URL 和查询字符串(如果指定)。例如, http://host/john/page.aspx 应该重定向到 http:
我遇到了以下问题: 我的 Grails (2.2.0) 应用程序具有以下 URL 映射: "/api/clientQuote/$labcode/$cliCode/$quoCode"(controlle
我有一个很长的 URL,它不适合 URL 字段。它一直在修剪。该怎么办?有没有办法增加 SharePoint 2010 中的 URL 字段字符限制? 或者解决方法来容纳长 URL。例如,以下 URL
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
我们从客户以前的开发人员那里继承了相当多的 Google Apps 脚本项目。 Apps 脚本通过嵌入式小部件部署在 Google 网站 (sites.google.com) 的各个页面上。每当我们需
我正在编写一些文档,但遇到了一些词汇问题: http://www.example.com/en/public/img/logo.gif 被称为“绝对”网址,对吗? ../../public/img/l
我们从客户以前的开发人员那里继承了相当多的 Google Apps 脚本项目。 Apps 脚本通过嵌入式小部件部署在 Google 网站 (sites.google.com) 的各个页面上。每当我们需
我是一名优秀的程序员,十分优秀!