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 myself_edit(request): print("--1") user = User.objects.get(username=request.user.username) userprofile = UserProfile.objects.get(user=request.user) userinfo = UserInfo.objects.get(user=request.user) print("--2") if request.method == "POST": print("--3") user_form = UserForm(request.POST) userprofile_form = UserProfileForm(request.POST) userinfo_form = UserInfoForm(request.POST) if user_form.is_valid() * userprofile_form.is_valid( ) * userinfo_form.is_valid(): user_cd = user_form.cleaned_data userprofile_cd = userprofile_form.cleaned_data userinfo_cd = userinfo_form.cleaned_data print("email", user_cd["email"]) user.email = user_cd["email"] print("birth", userprofile_cd["birth"]) userprofile.birth = userprofile_cd['birth'] userprofile.phone = userprofile_cd['phone'] userinfo.school = userinfo_cd['school'] userinfo.company = userinfo_cd['company'] userinfo.profession = userinfo_cd['profession'] userinfo.address = userinfo_cd['address'] userinfo.aboutme = userinfo_cd['aboutme'] user.save() userprofile.save() userinfo.save() return HttpResponseRedirect('/account/my-information/') else: user_form = UserForm(instance=request.user) userprofile_form = UserProfileForm(initial={ "birth": userprofile.birth, "phone": userprofile.phone }) userinfo_form = UserInfoForm( initial={ "school": userinfo.school, "profession": userinfo.profession, "company": userinfo.company, "address": userinfo.address, "aboutme": userinfo.aboutme, "school": userinfo.school }) return render( request, "account/myself_edit.html", { "user_form": user_form, "userprofile_form": userprofile_form, "userinfo_form": userinfo_form })
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 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 update_P_profile(request): json_data=status.objects.filter(status='ERR',MSG=None) errors="" if request.method == 'POST': user_form = UpdateUserForm(request.POST, instance=request.user) userprofile_form = UserProfileForm(request.POST, instance=request.user) userprofile_old = UserProfile.objects.get(user=request.user) user_old = request.user if userprofile_form.is_valid() and user_form.is_valid() : userprofile_old.phone_num1 = userprofile_form.cleaned_data['phone_num1'] userprofile_old.phone_num2 = userprofile_form.cleaned_data['phone_num2'] userprofile_old.address = userprofile_form.cleaned_data['address'] userprofile_old.birthday = userprofile_form.cleaned_data['birthday'] userprofile_old.area_id = userprofile_form.cleaned_data['area_id'] userprofile_old.save() user_old.first_name = user_form.cleaned_data['first_name'] user_old.last_name = user_form.cleaned_data['last_name'] user_old.email = user_form.cleaned_data['email'] user_old.save() json_data = status.objects.filter(status='OK') else: json_data = status.objects.filter(status='WRN') errors = ",[" + str(dict([(k, v[0].__str__()) for k, v in user_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 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 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 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 myself_edit(request): user = User.objects.get(username=request.user.username) userprofile = UserProfile.objects.get(user=user) userinfo = UserInfo.objects.get(user=user) if request.method == 'POST': user_form = UserForm(request.POST) userprofile_form = UserProfileForm(request.POST) userinfo_form = UserInfoForm(request.POST) if user_form.is_valid() * userprofile_form.is_valid() * userinfo_form.is_valid(): user_cd = user_form.cleaned_data userprofile_cd = userprofile_form.cleaned_data userinfo_cd = userinfo_form.cleaned_data user.email = user_cd['email'] userprofile.birth = userprofile_cd['birth'] userprofile.phone = userprofile_cd['phone'] userinfo.school = userinfo_cd['school'] userinfo.company = userinfo_cd['company'] userinfo.profession = userinfo_cd['profession'] userinfo.address = userinfo_cd['address'] userinfo.about_me = userinfo_cd['about_me'] user.save() userprofile.save() userinfo.save() return HttpResponseRedirect('/account/my-information/') else: user_form = UserForm(instance=request.user) userprofile_form = UserProfileForm(initial={'birth': userprofile.birth, 'phone': userprofile.phone}) userinfo_form = UserInfoForm(initial={'school': userinfo.school, 'company': userinfo.company, 'profession': userinfo.profession, 'address': userinfo.address, 'about_me': userinfo.about_me}) return render(request, 'account/myself_edit.html', {'user_form': user_form, 'userprofile_form': userprofile_form, 'userinfo_form': userinfo_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 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 myself_edit(request): userprofile = UserProfile.objects.get(user=request.user) if \ hasattr(request.user, 'userprofile') else UserProfile.objects.create(user=request.user) userinfo = UserInfo.objects.get(user=request.user) if hasattr(request.user, "userinfo") \ else UserInfo.objects.create(user=request.user) if request.method == "POST": user_form = UserForm(request.POST) userprofile_form = UserProfileForm(request.POST) userinfo_form = UserInfoForm(request.POST) if user_form.is_valid() * userprofile_form.is_valid( ) * userinfo_form.is_valid(): user_cd = user_form.cleaned_data userinfo_cd = userinfo_form.cleaned_data userprofile_cd = userprofile_form.cleaned_data request.user.email = user_cd["email"] userprofile.birth = userprofile_cd["birth"] userprofile.phone = userprofile_cd["phone"] userinfo.school = userinfo_cd["school"] userinfo.company = userinfo_cd["company"] userinfo.profession = userinfo_cd["profession"] userinfo.address = userinfo_cd["address"] userinfo.aboutme = userinfo_cd["aboutme"] request.user.save() userprofile.save() userinfo.save() return HttpResponseRedirect('/account/my-information/') else: user_form = UserForm(instance=request.user) userprofile_form = UserProfileForm(initial={ "birth": userprofile.birth, "phone": userprofile.phone }) userinfo_form = UserInfoForm( initial={ "school": userinfo.school, "company": userinfo.company, "profession": userinfo.profession, "address": userinfo.address, "aboutme": userinfo.aboutme }) return render( request, "account/myself_edit.html", { "user_form": user_form, "userprofile_form": userprofile_form, "userinfo_form": userinfo_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 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_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 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 post(self, request): up_form = UserProfileForm(request.POST) if not up_form.is_valid(): print(up_form.data) print(up_form.clean()) return redirect("/") if up_form.data['password'] != up_form.data['password2']: return redirect("/") hash_password = hash_str(up_form.cleaned_data['password'], config.password_salt) up = UserProfile.objects.create( username=up_form.cleaned_data['username'], password=hash_password, phone=up_form.cleaned_data['phone'], ) up.save() return redirect("/login")
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 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 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): # 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 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): 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 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 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): 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 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 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 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 HttpResponseRedirect(reverse("account:user_login")) else: return HttpResponse("sorry,your can not register!") else: user_form = RegistrationForm userprofile_form = UserProfileForm return render(request, "account/register.html", { "form": user_form, "profile": userprofile_form })
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 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 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 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 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): 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": 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 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 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 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 profile(request): """ This view show user`s profile page and it allows the users update their profiles Works of this view mainly if request method is "POST", it takes all data and looks they are valid or not if form is valid data are saving the table which are user_profile and user in the database :param request: :return: it returns profile page with users information """ # try: user_profile = request.user.get_profile() if request.method == "POST": data = request.POST.copy() new_user_form = UserForm(data) new_user_profile_form = UserProfileForm(data) # checking form validation if new_user_form.is_valid() and new_user_profile_form.is_valid(): picture = request.FILES.get("picture") # checking email change or not # if email was changed, new activation # code will send the new email address if user_profile.user.email != data["email"]: activation_key = tasks.generate_activation_key( user_profile.user.username ) key_expires_date = tasks.generate_key_expires_date() tasks.send_activation_code.delay( activation_key, data["email"] ) user_profile.key_expires = key_expires_date user_profile.activation_key = activation_key user_profile.user.is_active = False user_profile.user.email = data["email"] user_profile.user.first_name = data["first_name"] user_profile.user.last_name = data["last_name"] user_profile.birth_date = data["birth_date"] user_profile.url = data["url"] # checking picture if it is not empty # delete previous one if picture is not None: if picture is not "not-found.png": user_profile.picture.delete(save=False) user_profile.picture = picture # saving new user profile and this profile`s user user_profile.save() user_profile.user.save() # determining success message messages.success( request, _('Profile details updated successfully.'), fail_silently=True ) # if profile saves successfully, # profile picture resizing with pil by using celery task if picture is not None: tasks.resize_image.delay(user_profile.picture, 240, 240) # sending form to template with initial data # because i do not send any extra data to view # it needs first data new_user_form = UserForm(initial={ "email": user_profile.user.email, "username": user_profile.user.username, "first_name": user_profile.user.first_name, "last_name": user_profile.user.last_name} ) new_user_profile_form = UserProfileForm(initial={ "picture": user_profile.picture, "url": user_profile.url, "birth_date": user_profile.birth_date} ) else: user_profile = request.user.get_profile() # determining error message messages.error( request, _('Profile details failed.'), fail_silently=True ) new_user_form = UserForm(data) new_user_profile_form = UserProfileForm(data) else: user_profile = request.user.get_profile() # user_profile = UserProfile.objects.get(slug=slug) new_user_form = UserForm(initial={ "email": user_profile.user.email, "username": user_profile.user.username, "first_name": user_profile.user.first_name, "last_name": user_profile.user.last_name} ) new_user_profile_form = UserProfileForm(initial={ "picture": user_profile.picture, "url": user_profile.url, "birth_date": user_profile.birth_date} ) c = {"user_profile": user_profile, "user_profile_form": new_user_profile_form, "user_form": new_user_form} if user_profile is not None: return render(request, "profile.html", c) else: return redirect(reverse('index'))