我正在尝试密码保护Django中的注册页面,而无需用户登录,但我似乎无法弄明白。我的流程应该是:
UserCreationForm
表格UserCreationForm
UserCreationForm
,系统会再次向用户显示UserCreationForm
+错误views.py
填写正确,则会将用户重定向到其个人资料页面我现在遇到的问题是我无法将用户重定向到没有URL的视图(包含UserCreationForm的视图)。
这是我的代码:
def register(request):
if request.method == 'POST':
# Gather information from all forms submitted
user_custom_info = user_information(request.POST)
user_info = UserCreationForm(request.POST)
profile_info = deejay_form(request.POST)
# Check to make sure they entered data into each of the forms
info_validated = user_info.is_valid() and user_custom_info.is_valid() and profile_info.is_valid()
# If they did...
if info_validated:
# Clean the data...
user_custom_info = user_custom_info.cleaned_data
user_info = user_info.cleaned_data
profile_info = profile_info.cleaned_data
# Create a new user with those traits
new_user = User.objects.create_user(user_info['username'], user_custom_info['email'], user_info['password1'])
new_user.first_name = user_custom_info['first_name']
new_user.last_name = user_custom_info['last_name']
new_user.save()
# Create a new deejay with those traits..
new_deejay = Deejay(user=new_user, dj=profile_info['dj'], role=profile_info['role'], bio=profile_info['bio'], phone=profile_info['phone'])
new_deejay.save()
# Log in the user..
if not request.user.is_authenticated():
this_user = authenticate(username=user_info['username'], password=user_info['password1'])
login(request, this_user)
# Need to add to group - http://stackoverflow.com/questions/6288661/adding-a-user-to-a-group-in-django
# Redirect to dj page
return redirect('dj_detail', dj_name=Deejay.objects.get(user=request.user).dj)
else:
return render(request, 'pages/backend/register.html', {'forms':[user_custom_info, user_info, profile_info]})
return render(request, 'pages/backend/register.html', {'forms':[user_information, UserCreationForm, deejay_form]})
# View for a password protected registration form
def register_protect(request):
if request.method == 'POST':
pw_info = registration_access(request.POST)
if pw_info.is_valid():
return redirect(register)
else:
return render(request, 'pages/backend/register.html', {'forms':[pw_info]})
return render(request, 'pages/backend/register.html', {'forms':[registration_access]})
forms.py
class user_information(forms.ModelForm):
first_name = forms.CharField(label='First Name', required=True)
last_name = forms.CharField(label='Last Name', required=True)
email = forms.EmailField(label='Email', required=True)
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
class deejay_form(forms.ModelForm):
class Meta:
model = Deejay
fields = ('dj', 'role', 'bio', 'phone')
class registration_access(forms.Form):
secret_password = forms.CharField(label="Secret Password", widget=forms.PasswordInput())
def clean(self):
access_password = "mypassword"
given_password = self.cleaned_data.get('secret_password')
if given_password != access_password:
raise forms.ValidationError("Did you forget your training?")
return self.cleaned_data
{{1}}
答案 0 :(得分:0)
“重定向”意味着,根据定义,重定向回服务器。因此,您需要重定向到URL。您可以重定向到相同的URL,但是您需要编写视图才能处理您想要执行的不同操作。
对我而言,听起来您可以更好地使用Javascript并将其作为单页应用处理。