gpt4 book ai didi

python - 如何在pinax中调用一个url的多个 View

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:53 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com