私はDjangoベースのサイトを持っています。これにより、ユーザーは登録できますが、サイトの特定の部分を表示するには管理者がアカウントを承認する必要があります。私はdjango.contrib.auth
の外にそれを置いています。私はユーザーに特定のドメイン名の電子メールアドレスを登録する必要があるので、UserCreationForm
のsave()
とclean_email()
メソッドをオーバーライドしました。Djangoでこの「登録」ビューを改善するにはどうすればよいですか?
私の登録ページは以下のビューを使用しています。私はこのビューコードの改善やプロセスの改善(または他の何か、本当に)をどのように改善するかについて聞いてみたいと思います。
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
message = None
form.save()
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(username=username, password=password)
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
email = user.email
# If valid new user account
if (user is not None) and (user.is_active):
login(request, user)
message = "<strong>Congratulations!</strong> You have been registered."
# Send emails
try:
# Admin email
pk = None
try: pk = User.objects.filter(username=username)[0].pk
except: pass
admin_email_template = loader.get_template('accounts/email_notify_admin_of_registration.txt')
admin_email_context = Context({
'first_name': first_name,
'last_name': last_name,
'username': username,
'email': email,
'pk': pk,
})
admin_email_body = admin_email_template.render(admin_email_context)
mail_admins("New User Registration", admin_email_body)
# User email
user_email_template = loader.get_template('accounts/email_registration_success.txt')
user_email_context = Context({
'first_name': form.cleaned_data['first_name'],
'username': username,
'password': password,
})
user_email_body = user_email_template.render(user_email_context)
user.email_user("Successfully Registered at example.com", user_email_body)
except:
message = "There was an error sending you the confirmation email. You should still be able to login normally."
else:
message = "There was an error automatically logging you in. Try <a href=\"/login/\">logging in</a> manually."
# Display success page
return render_to_response('accounts/register_success.html', {
'username': username,
'message': message,
},
context_instance=RequestContext(request)
)
else: # If not POST
form = UserCreationForm()
return render_to_response('accounts/register.html', {
'form': form,
},
context_instance=RequestContext(request)
)
私のサイトには、たくさんのトラフィックが見られることは間違いありません。それは小さなクラブのためのものですが、残りの提案に感謝します!彼らはとても役に立ちました。 – Tyson
アップ票と受け入れられた答えをありがとうが、あなたの質問は簡単なものではありません...私に今すぐ投票を与え、他の人が応える時間を増やしたいと思うかもしれません。他の人がもっと良い答えを持っているかもしれません! :) –