def edit_profile(request, pk): user = request.user if pk is None else User.objects.get(pk=pk) # user = request.user.userprofile if request.method == 'GET': form = UserProfileForm(instance=user.userprofile) context = { 'user': user, 'form': form, } return render(request, 'accounts/edit_profile.html', context) else: form = UserProfileForm(request.POST, request.FILES, instance=user.userprofile) if form.is_valid(): form.save() return redirect('user profile') context = { 'user': user, 'form': form, } return render(request, 'accounts/edit_profile.html', context)
def user_profile(request): user = User.objects.get(id=request.user.id) profile = None try: profile = Profile.objects.get(user=user) except Exception as e: print(f'Exception error: {e}') if request.method == 'POST': profile_form = UserProfileForm(instance=profile, data=request.POST) # profile_form = None if profile_form.is_valid(): print('a') profile_form = profile_form.save(commit=False) # profile_form profile_form.user = user profile_form.save() # track_action(profile_form.user, 'has updated profile') return render(request, 'account/profile_done.html') else: if profile: profile_form = UserProfileForm(instance=profile) else: profile_form = UserProfileForm() user_orders = Order.objects.filter(user=request.user) return render(request, 'account/profile.html', { 'profile_form': profile_form, 'user_orders': user_orders })
def image_upload(request): if request.method == 'POST': form = UserProfileForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('account:profile') else: form = UserProfileForm() return render(request, 'account/image_upload.html', {'form': form})
def form_valid(self, form): user = form.save() user_profile_form = UserProfileForm(self.request.POST, self.request.FILES, instance=self.request.user.get_profile()) if user_profile_form.is_valid(): user_profile_form.save() else: return render_to_response('auth/user_update_form.html', {'object' : user, 'form': form, 'user_profile_form' : user_profile_form}, context_instance=RequestContext(self.request)) if form.cleaned_data['new_password']: user.set_password(form.cleaned_data['new_password']) user.save() return HttpResponseRedirect(reverse('edit_profile'))
def register(request): ''' Register a new user ''' template = 'account/register.html' if request.method == 'GET': return render(request, template, { 'userForm': UserForm(), 'userProfileForm': UserProfileForm() }) # POST userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if not userForm.is_valid() or not userProfileForm.is_valid(): return render(request, template, { 'userForm': userForm, 'userProfileForm': userProfileForm }) user = userForm.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, '歡迎加入') return redirect('main:main')
def register(request): if request.method == "POST": user_form_data = RegistrationForm(request.POST) user_profile_form_data = UserProfileForm(request.POST) if user_form_data.is_valid() & user_profile_form_data.is_valid(): new_form = user_form_data.save(commit=False) new_form.set_password(user_form_data.cleaned_data["password"]) new_form.save() new_profile = user_profile_form_data.save(commit=False) new_profile.user = new_form new_profile.save() UserInfo.objects.create(user=new_form) return HttpResponse( "successfully register ,you can <a href='{%url 'account:user_login'%}'>login now</a>" ) else: return HttpResponse("sorry") else: user_register_form = RegistrationForm() user_profile_form = UserProfileForm() return render( request, "account/register.html", { "user_register_form": user_register_form, "user_profile_form": user_profile_form })
def post(self, request, *args, **kwargs): userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if request.POST.get('betacode') != "Ew0Xav08L4": kwargs['error'] = "* 註冊碼錯誤" kwargs['userForm'] = userForm kwargs['userProfileForm'] = userProfileForm return super(SignUp, self).post(request, *args, **kwargs) if not (userForm.is_valid() and userProfileForm.is_valid()): kwargs['userForm'] = userForm kwargs['userProfileForm'] = userProfileForm return super(SignUp, self).post(request, *args, **kwargs) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.type = 1 #2 = Manager userProfile.isAuth = False userProfile.save() #messages.success(request, '註冊成功') messages.success(request, '註冊成功') return redirect(reverse('main:main'))
def register(request): ''' Register a new user ''' template = 'account/register.html' if request.method == 'GET': return render(request, template, { 'userForm': UserForm(), 'userProfileForm': UserProfileForm() }) # POST userForm = UserForm(request.POST) # Create your views here. userProfileForm = UserProfileForm(request.POST) if not (userForm.is_valid() and userProfileForm.is_valid()): return render(request, template, { 'userForm': userForm, 'userProfileForm': userProfileForm }) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, '歡迎註冊') return redirect('main:main')
def post(self, request, *args, **kwargs): user_form = UserForm(data=request.POST, prefix='user') profile_form = UserProfileForm(data=request.POST, prefix='profile') 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.userprofile = 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 'profile-userprofile_picture' in request.FILES: profile.userprofile_picture = request.FILES['profile-userprofile_picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True return HttpResponseRedirect('/order/') else: return HttpResponse('%s %s' % (user_form, profile_form))
def register(request): # if request.method == 'POST': # data = request.POST # username = data.get('username') # password1 = data.get('password1') # password2 = data.get('password2') # birth = data.get('birth') # phone = data.get('phone') # email = data.get('email') # 使用form完成注册 if request.method == 'POST': user_form = RegisterForm(request.POST) user_profile_form = UserProfileForm(request.POST) new_user = user_form.save(commit=False) # 从user_form中提取password1 new_user.set_password(user_form.cleaned_data['password1']) # 保存user new_user.save() new_user_profile = user_profile_form.save(commit=False) # 关联user_profile 和 user new_user_profile.user = new_user # 保存user_profile new_user_profile.save() from account.models import UserInfo UserInfo.objects.create(user=new_user) return redirect(reverse("account:user_login")) if request.method == 'GET': # 把注册表单发送给用户 return render(request, 'account/register.html')
def reigster(request): #init password= qwertasdfg if request.method == "POST": user_form = RegistrationForm(request.POST) userprofile_form = UserProfileForm(request.POST) if user_form.is_valid(): # print(user_form.cleaned_data['username']) # print(user_form.cleaned_data['email']) print(user_form.cleaned_data['password']) print(user_form.cleaned_data['password2']) new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() new_profile = userprofile_form.save(commit=False) new_profile.user = new_user new_profile.save() UserInfo.objects.create(user=new_user) return HttpResponse("successfully") else: return HttpResponse("<h1>Sorry, you cannot register</h1>") if request.method == "GET": user_form = RegistrationForm() userprofile_form = UserProfileForm() return render(request, "account/register.html", { "form": user_form, "profile": userprofile_form })
def register(request): ''' 注册 :param request: :return: ''' if request.method == "POST": print(request.POST) user_form = RegisterForm(request.POST) userpro_form = UserProfileForm(request.POST) if user_form.is_valid() and userpro_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() new_profile = userpro_form.save(commit=False) new_profile.user = new_user new_profile.save() return HttpResponse("register success") else: return HttpResponse("register fail") else: user_form = RegisterForm() userpro_form = UserProfileForm() return render(request, 'mybuggreport/register.html', { "form": user_form, "user_form": userpro_form })
def user_profile(request): user_profile = get_object_or_404(UserProfile, user=request.user) if request.method == "POST": profile_form = UserProfileForm(request.POST, request.FILES, instance=user_profile) if profile_form.is_valid(): profile = profile_form.save() messages.info(request, _("profile has been updated")) avatar = profile.avatar.path crop_image.delay(avatar) return HttpResponseRedirect(reverse('user_profile')) else: messages.error(request, _("profile update could not be completed")) ctx = {"profile_form": profile_form} return render(request, "user_profile.html", ctx) else: user_post = Post.objects.filter(user=request.user) profile_form = UserProfileForm(instance=user_profile) password_change_form = UserPasswordChangeForm() ctx = { "profile_form": profile_form, "posts": user_post, "password_change_form": password_change_form } return render(request, "user_profile.html", ctx)
def register(request): if request.method == "POST": user_form = RegistrationForm(request.POST) userprofile_form = UserProfileForm(request.POST) print(user_form) print("user验证:", user_form.is_valid()) print(userprofile_form) print("profile验证:", userprofile_form.is_valid()) if user_form.is_valid() and userprofile_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() new_userprofile = userprofile_form.save(commit=False) new_userprofile.user = new_user new_userprofile.save() return HttpResponse("成功") else: return render(request, "account/register2.html", { "form": user_form, "profile": userprofile_form }) else: user_form = RegistrationForm() userprofile_form = UserProfileForm() return render(request, "account/register2.html", { "form": user_form, "profile": userprofile_form })
def register(request): if request.user.is_authenticated(): return HttpResponse("You are already logged in.") else: # 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, 'account/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def profile(request): profile = request.user.profile if request.POST: user_form = UserForm(request.POST, instance=request.user) user_profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and user_profile_form.is_valid(): user_form.save() user_profile_form.save() else: user_form = UserForm(instance=request.user) user_profile_form = UserProfileForm(instance=profile) context = { 'user_form': user_form, 'user_profile_form': user_profile_form, 'profile': profile } return render(request, 'account/profile.html', context)
def edit(request, user_id): user = get_object_or_404(User, pk=user_id) profile = user.profile if request.POST: user_form = UserForm(request.POST, instance=user) user_profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and user_profile_form.is_valid(): user_form.save() user_profile_form.save() else: user_form = UserForm(instance=user) user_profile_form = UserProfileForm(instance=profile) context = { 'user_form': user_form, 'user_profile_form': user_profile_form, 'profile': profile } return render(request, 'account/profile.html', context)
def update(request): userprofile, t = UserProfile.objects.get_or_create(user=request.user) if request.POST: useremail_form = UserEmailForm(request.POST, instance=request.user) userprofile_form = UserProfileForm(request.POST, instance=userprofile) if useremail_form.is_valid() and userprofile_form.is_valid(): useremail_form.save() userprofile_form.save() return HttpResponseRedirect(reverse('profile_update')) else: useremail_form = UserEmailForm(instance=request.user) userprofile_form = UserProfileForm(instance=userprofile) return { 'useremail_form':useremail_form, 'userprofile_form':userprofile_form }
def update_profile(request): if request.method == 'POST': # user_form = RegisterForm(request.POST,instance=request.user) profile_form = UserProfileForm(request.POST,files=request.FILES, instance=request.user) if profile_form.is_valid(): profile_form.save() messages.success(request, _('Your profile was successfully updated!')) return redirect ('account:view_my_ads', user_id = request.user.id ) else: messages.error(request, _('Please correct the error below.')) else: profile_form = UserProfileForm(instance=request.user.client) return render(request, 'profile/edit_profile.html', { # 'user_form': user_form, 'profile_form': profile_form, 'local_css_urls': settings.SB_ADMIN_2_CSS_LIBRARY_URLS, 'local_js_urls': settings.SB_ADMIN_2_JS_LIBRARY_URLS, })
def register(request): # user_form = UserCreationForm() user_form = RegisterForm() user_profile_form = UserProfileForm() if request.POST: user_form = RegisterForm(request.POST) if user_form.is_valid(): u = user_form.save() g = Group.objects.get(name='player') g.user_set.add(u) profile = u.profile user_profile_form = UserProfileForm(request.POST, instance=profile) user_profile_form.save() return HttpResponseRedirect(reverse('account:register')) context = { 'user_form': user_form, 'user_profile_form': user_profile_form, } return render(request, 'account/register.html', context)
def Employee_edit_profile(request): instance = request.user.userprofile form = UserProfileForm(instance=instance) if request.method == 'POST': form = UserProfileForm(request.POST, request.FILES, instance=instance) if form.is_valid(): instance = request.user.userprofile instance = form.save(commit=False) instance.save() return HttpResponseRedirect('/Student/homepage/') context = {'form': form} return render(request, "Boss/editprofile.html", context)
def user_profile(request, pk=None): user = request.user if pk is None else User.objects.get(pk=pk) if request.method == 'GET': context = { 'form': UserProfileForm(), 'profile_user': user, 'profile': user.userprofile, 'posts': user.userprofile.post_set.all(), } return render(request, 'accounts/user_profile.html', context) else: form = UserProfileForm(request.POST, request.FILES, instance=user.userprofile) if form.is_valid(): form.save() return redirect('user profile') context = { 'form': form, } return render(request, 'accounts/edit_profile.html', context)
def profile(request): user = request.user if request.method == 'POST': form = UserProfileForm(request.POST, instance=user) if form.is_valid(): user = form.save(commit=True) return render(request, 'setting/profile.html', {'user_form': form, 'alert_message': 'Profile updated successfully — view your profile.'}) else: form = UserProfileForm(instance=user) return render(request, 'setting/profile.html', {'user_form': form})
def edit_profile(request, user_id): try: user = User.objects.get(pk=user_id) form = UserProfileForm(request.POST, request.FILES, instance=user.profile, user=request.user) valid = form.is_valid() if valid: profile = form.save() return JsonResponse(profile.to_user()) else: return JsonResponse({'errors': form.errors}, status=400) except User.DoesNotExist: return JsonResponse({'errors': 'cannot find user with that id'}, status=400)
def register(request): template = "account/register.html" if request.method == "GET": return render(request, template, {"userForm": UserForm(), "userProfileForm": UserProfileForm()}) # request.method == 'POST': userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if not (userForm.is_valid() and userProfileForm.is_valid()): return render(request, template, {"userForm": userForm, "userProfileForm": userProfileForm}) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, "歡迎註冊") return redirect(reverse("main:main"))
def register(request): template = 'account/register.html' if request.method=='GET': return render(request, template, {'userForm':UserForm(),'userProfileForm':UserProfileForm()}) # request.method == 'POST': userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if not (userForm.is_valid() and userProfileForm.is_valid()): return render(request, template, {'userForm':userForm,'userProfileForm':userProfileForm}) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, '歡迎註冊') return redirect(reverse('main:main'))
def signup(request): if request.method == 'POST': form1 = SignUpForm(request.POST) form2 = UserProfileForm(request.POST) if form1.is_valid() and form2.is_valid(): form1.save() username = form1.cleaned_data.get('username') raw_password = form1.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) login(request, user) profile = form2.save(commit=False) profile.user = user profile.save() return redirect('account:profile') else: form1 = SignUpForm() form2 = UserProfileForm() return render(request, 'account/signup.html', {'form1': form1, 'form2':form2})
def register(request): template = 'account/register.html' if request.method=='GET': return render(request, template, {'userForm':UserForm(), 'userProfileForm':UserProfileForm()}) # request.method == 'POST': userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if not (userForm.is_valid() and userProfileForm.is_valid()): return render(request, template, {'userForm':userForm, 'userProfileForm':userProfileForm}) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, '歡迎登入') return redirect(reverse('main:main'))
def register(request): if request.method == "POST": form = RegisterForm(request.POST) profile_form = UserProfileForm(request.POST) if form.is_valid() and profile_form.is_valid(): user = form.save() profile = profile_form.save(commit=False) profile.user = user profile.save() return redirect("/") else: print(form.errors) else: form = RegisterForm() profile_form = UserProfileForm() return render(request, 'registration/registration.html', { "form": form, 'profile_form': profile_form })
def register(request): if request.method == 'POST': user_form = RegistrationForm(request.POST) userprofile_form = UserProfileForm(request.POST) if user_form.is_valid() * userprofile_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() new_profile = userprofile_form.save(commit=False) new_profile.user = new_user new_profile.save() UserInfo.objects.create(user=new_user) # return HttpResponse('Successfully') return HttpResponseRedirect(reverse('account:user_login')) else: return HttpResponse('Sorry, you can not register.') else: user_form = RegistrationForm() userprofile_form = UserProfileForm() return render(request, 'account/register.html', {'form': user_form, 'profile': userprofile_form})
def register(request): if request.method == 'POST': user_form = RegistrationForm(request.POST) userprofile_form = UserProfileForm(request.POST) if user_form.is_valid() and userprofile_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() new_profile = userprofile_form.save(commit=False) new_profile.user = new_user new_profile.save() return redirect('account:user_login') else: return redirect('account:user_register') else: user_form = RegistrationForm() user_profile = UserProfileForm() return render(request, 'account/register.html', { 'user_form': user_form, 'user_profile': user_profile })
def register(request): if request.method == "POST": print(request.POST) user_form = RegistrationForm(request.POST) userprofile_form = UserProfileForm(request.POST) print(user_form) print(userprofile_form) if user_form.is_valid() and userprofile_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) # p64 new_user.save() new_profile = userprofile_form.save(commit=False) new_profile.user = new_user new_profile.save() return HttpResponse('注册成功') else: return HttpResponse('对不起,您输入格式不对,无法注册' ) else: user_form = RegistrationForm() userprofile_form = UserProfileForm() return render(request,'account/register.html',{'form':user_form,'profile':userprofile_form})
def user_profile(request): user_profile = get_object_or_404(UserProfile, user=request.user) if request.method == "POST": profile_form = UserProfileForm(request.POST,request.FILES, instance=user_profile) if profile_form.is_valid(): profile = profile_form.save() messages.info(request, _("profile has been updated")) avatar = profile.avatar.path crop_image.delay(avatar) return HttpResponseRedirect(reverse('user_profile')) else: messages.error(request, _("profile update could not be completed")) ctx = {"profile_form": profile_form} return render(request, "user_profile.html", ctx ) else: user_post = Post.objects.filter(user=request.user) profile_form = UserProfileForm(instance=user_profile) password_change_form = UserPasswordChangeForm() ctx = {"profile_form": profile_form, "posts": user_post, "password_change_form": password_change_form } return render(request, "user_profile.html", ctx)
def register(request): if request.method == "POST": user_form = RegistrationForm(request.POST) userprofile_form = UserProfileForm(request.POST) if user_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() new_profile = userprofile_form.save(commit=False) new_profile.user = new_user new_profile.save() return HttpResponse("successfully") else: return HttpResponse("sorry,you can't register") else: user_form = RegistrationForm() userprofile_form = UserProfileForm() return render(request, 'account/register.html', { "form": user_form, "profile": userprofile_form })
def register(request): context = {} registered = False if request.method == 'POST': user_form = UserForm(request.POST, prefix='user') profile_form = UserProfileForm(request.POST, prefix='profile') 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_profile = user if 'profile-user_profile_picture' in request.FILES: profile.user_profile_picture = request.FILES['profile-user_profile_picture'] profile.save() # Flag para el formulario de registro registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm(prefix='user') profile_form = UserProfileForm(prefix='profile') context.update({'user_form': user_form, 'profile_form': profile_form, 'registered': registered}) return render(request, 'account_register.html', context)
def home(request): try: profile = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: profile = None try: userviewanswer = UserViewSuggestion.objects.get(user=request.user) except UserViewSuggestion.DoesNotExist: userviewanswer = None if (request.method=="POST"): if userviewanswer: answerform = UserViewSuggestionForm(request.POST,instance = userviewanswer) else: answerform = UserViewSuggestionForm(request.POST) if profile: userform = UserProfileForm(request.POST) else: userform = UserProfileForm(request.POST,instance = profile) if answerform.is_valid() and userform.is_valid(): answer = answerform.save(commit=False) profile = userform.save(commit=False) answer.user = request.user answer.save() profile.user = request.user profile.save() answerform.save_m2m() messages.success(request, 'Your suggestion has been taken') else: messages.error(request, 'Please enter all required fields') else: if userviewanswer: answerform = UserViewSuggestionForm(instance = userviewanswer) else: answerform = UserViewSuggestionForm() if profile: userform = UserProfileForm(instance = profile) else: userform = UserProfileForm() return render(request,'userview/home.html',{'answerform':answerform,'userform':userform})
def register(request): if request.method == 'POST': user_form = RegistrationForm(request.POST) user_profile_form = UserProfileForm(request.POST) if user_form.is_valid() and user_profile_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() new_profile = user_profile_form.save(commit=False) new_profile.user = new_user new_profile.save() UserInfo.objects.create(user=new_user) return HttpResponseRedirect(reverse("account:user_login")) else: return HttpResponse("sorry, your can not register") else: user_form = RegistrationForm() user_profile_form = UserProfileForm() return render(request, "account/register.html", { "form": user_form, "profile": user_profile_form })
def register_view(request): context = RequestContext(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() messages.success(request,'Successfully Registered') registered=True return HttpResponseRedirect('/guide/index.html') else: user_form=UserForm() profile_form=UserProfileForm() return render_to_response('guide/home.html',{'user_form': user_form, 'profile_form': profile_form,'registered': registered},context)
def create_P_user(request): json_data=status.objects.filter(status='ERR',MSG=None) errors="" if request.method == 'POST': userprofile_form = UserProfileForm(request.POST) user_form = UserForm(request.POST) if userprofile_form.is_valid() and user_form.is_valid(): user_clean_data = user_form.cleaned_data created_user = User.objects.create_user(user_clean_data['username'], user_clean_data['email'], user_clean_data['password1']) created_user.first_name=request.POST['firstname'] created_user.last_name=request.POST['lastname'] created_user.save() userprofile = userprofile_form.save(commit=False) userprofile.user = created_user userprofile.level = 0 userprofile.is_client = True userprofile.phone_num1 = userprofile_form.cleaned_data['phone_num1'] userprofile.phone_num2 = userprofile_form.cleaned_data['phone_num2'] userprofile.address = userprofile_form.cleaned_data['address'] userprofile.birthday = userprofile_form.cleaned_data['birthday'] userprofile.area_id = userprofile_form.cleaned_data['area_id'] userprofile.save() new_user = authenticate(username=request.POST['username'], password=request.POST['password1']) login(request, new_user) json_data = status.objects.filter(status='OK') else: json_data = status.objects.filter(status='WRN') if user_form.errors.items() : errors = ",[" + str(dict([(k, v[0].__str__()) for k, v in user_form.errors.items()])) + "]" if userprofile_form.errors.items(): errors += ",[" + str(dict([(k, v[0].__str__()) for k, v in userprofile_form.errors.items()])) + "]" else: json_data=list(status.objects.filter(status='ERR',MSG='PD')) json_dump = "[" + serializers.serialize("json", json_data) json_dump += errors + "]" return HttpResponse(json_dump.replace('\'','"'))
def create_user(request): if request.method == 'POST': agree_form = AgreeForm(request.POST) userprofile_form = UserProfileForm(request.POST) user_form = UserForm(request.POST) if userprofile_form.is_valid() and user_form.is_valid() and agree_form.is_valid(): user_clean_data = user_form.cleaned_data created_user = User.objects.create_user(user_clean_data['username'], user_clean_data['email'], user_clean_data['password1']) created_user.save() userprofile = userprofile_form.save(commit=False) userprofile.user = created_user userprofile.minLevel = 0 userprofile.isCustomer = True userprofile.phoneNum = userprofile_form.cleaned_data['phoneNum'] userprofile.save() new_user = authenticate(username=request.POST['username'], password=request.POST['password1']) login(request, new_user) return HttpResponseRedirect('/account/is_login') else: userprofile_form = UserProfileForm() user_form = UserForm() agree_form = AgreeForm() return render_to_response('registration/create_user.html', { 'userprofile_form': userprofile_form, 'user_form': user_form, 'agree_form': agree_form}, context_instance=RequestContext(request))
def register(request): # Like before, get the request's context. context = RequestContext(request) context_dict = {} # 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'] if profile_form.province: profile.province = profile_form.province if profile_form.city: profile.city = profile_form.city # 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() context_dict['user_form'] = user_form context_dict['profile_form'] = profile_form context_dict['registered'] = registered # Render the template depending on the context. return render_to_response('account/register.html', context_dict, context)