以下是 view.py,其中包括
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)
以下是皮纳克斯给出的
account/url.py
帐户/网址.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"),
)
以下是我的项目
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
.它在单个 html 中包含两个表单。
不要这样做。 尝试了解浏览器和网络服务器之间发生的事情。 URL 标识服务器将返回到客户端的内容。 称2个"观点"违反了这一原则。
您真正想做的是创建一个新的 URL,或修改模板以将两个表单放在同一个页面上。 我可能会重命名SignupOrLoginView并在该单个视图中进行注册工作。
这在功能视图中更容易理解,而不是基于类的视图,后者似乎将您限制在单个窗体中。