def register(request): registered = False if request.method == 'POST': user_form = UserForm(data = request.POST) profile_form = UserProfileForm(data = request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit = False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form' : user_form, 'profile_form' : profile_form, 'registered' : registered})
def register(request): registered = False if request.method == 'POST': user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print(user_form.erros) print(profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): dict = {} registed = False if (request.method == 'POST'): user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if (user_form.is_valid() and profile_form.is_valid()): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if ('picture' in request.FILES): profile.picture = request.FILES['picture'] profile.save() registed = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() dict['user_form'] = user_form dict['profile_form'] = profile_form dict['registed'] = registed return render(request, 'rango/register.html', dict)
def register(request): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def register(request): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def post(self, request): form = UserProfileForm(request.POST, request.FILES) if form.is_valid(): user_profile = form.save(commit=False) user_profile.user = request.user user_profile.save() return redirect('rango:index') else: print(form.errors) context_dict = {'form': form} return render(request, 'rango/profile_registration.html', context_dict)
def post(self, request, username): try: (user, userprofile, form) = self.get_user_details(username) except TypeError: return redirect('rango:index') if user == request.user: # Added for authentication exercise. form = UserProfileForm(request.POST, request.FILES, instance=userprofile) if form.is_valid(): form.save(commit=True) return redirect('rango:profile', user.username) else: print(form.errors) context_dict = { 'userprofile': userprofile, 'selecteduser': user, 'form': form } return render(request, 'rango/profile.html', context_dict)
def register(request): # 一个布尔值,告诉模板注册是否成功 # 一开始设为 False,注册成功后改为 True registered = False # 如果是 HTTP POST 请求,处理表单数据 if request.method == 'POST': # 尝试获取原始表单数据 # 注意,UserForm 和 UserProfileForm 中的数据都需要 user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # 如果两个表单中的数据是有效的…… if user_form.is_valid() and profile_form.is_valid(): # 把 UserForm 中的数据存入数据库 user = user_form.save() # 使用 set_password 方法计算密码哈希值 # 然后更新 user 对象 user.set_password(user.password) user.save() # 现在处理 UserProfile 实例 # 因为要自行处理 user 属性,所以设定 commit=False # 延迟保存模型,以防出现完整性问题 profile = profile_form.save(commit=False) profile.user = user # 用户提供头像了吗? # 如果提供了,从表单数据库中提取出来,赋给 UserProfile 模型 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # 保存 UserProfile 模型实例 profile.save() # 更新变量的值,告诉模板成功注册了 registered = True else: # 表单数据无效,出错了? # 在终端打印问题 print(user_form.errors, profile_form.errors) else: # 不是 HTTP POST 请求,渲染两个 ModelForm 实例 # 表单为空,待用户填写 user_form = UserForm() profile_form = UserProfileForm() # 根据上下文渲染模板 return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): if request.session.test_cookie_worked(): print ">>>> TEST COOKIE WORKED!" request.session.delete_test_cookie() registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register_profile(request): if request.method == 'POST': form = UserProfileForm(request.POST) if form.is_valid(): form.save(commit=True) return redirect('rango/index.html')