def user_profile(request): """User's profile page with auction results and profile form to update""" current_auctions = Auction.objects.filter( winning_bidder=request.user.pk, auction_end_time__gte=timezone.now()) past_paid_auctions = Auction.objects.filter( winning_bidder=request.user.pk, auction_end_time__lt=timezone.now(), paid=True) past_unpaid_auctions = Auction.objects.filter( winning_bidder=request.user.pk, auction_end_time__lt=timezone.now(), paid=False) if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, 'Your profile was successfully updated.') return redirect('profile.html') else: messages.error(request, 'Please correct the error below.') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render( request, 'profile.html', { 'user_form': user_form, 'profile_form': profile_form, 'current_auctions': current_auctions, 'past_paid_auctions': past_paid_auctions, 'past_unpaid_auctions': past_unpaid_auctions })
def update_profile(request): """ Fetch data from user and user profile and render it on the form. Update both models. """ if not request.user.is_authenticated: return redirect('login') else: if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profiles) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, ('Your profile has been updated successfully!')) return redirect('user_profile') else: messages.error(request, ('Please correct the error below.')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profiles) context = { 'user_form': user_form, 'profile_form': profile_form, 'title': 'Update', 'profile': 'profile' } return render(request, 'update_profile.html', context)
def profile(request): form = UserForm(instance=request.user) pwd_form = PasswordChangeForm(request.user) associations = {'twitter': False, 'google_oauth2': False, 'github': False} for association in request.user.social_auth.all(): associations[association.provider.replace('-', '_')] = True if request.method == 'POST': if request.POST.get('do_password'): pwd_form = PasswordChangeForm(request.user, request.POST) if pwd_form.is_valid(): pwd_form.save() messages.success(request, "Password successfully changed.") else: messages.error(request, "Could not update password. See errors below.") elif request.POST.get('do_profile'): form = UserForm(request.POST, instance=request.user) if form.is_valid(): form.save() messages.success(request, 'Profile successfully updated.') else: messages.error(request, 'You have an error in your profile. See below errors.') else: messages.error(request, "Er, something weird happened. Contact the site admin.") return render(request, 'accounts/profile.html', locals())
def dashboard(request): user = request.user try: user_profile = UserProfile.objects.get(user=user) except Exception: _profile(user) user_profile = UserProfile.objects.get(user=user) orders = Order.objects.order_by('-created_at').filter(user_id=request.user.id, is_ordered=True) orders_count = orders.count() if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = UserProfileForm(request.POST, request.FILES, instance=user_profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, 'Ваши данные успешно обновлены!') return redirect('dashboard') else: user_form = UserForm(instance=request.user) profile_form = UserProfileForm(instance=user_profile) context = { 'orders': orders, 'orders_count': orders_count, 'user_form': user_form, 'profile_form': profile_form, 'user_profile': user_profile, } return render(request, 'accounts/dashboard.html', context)
def profile(request): form = UserForm(instance=request.user) if request.method == 'POST': form = UserForm(instance=request.user, data=request.POST) if form.is_valid(): form.save() return render(request, 'profile.html', { 'form': form, 'locations': Location.objects.all()})
def user_profile(request): profile = request.user.get_profile() if request.method == "POST": # Read params form = ProfileForm(request.POST, instance=profile) subscriptionform = SubscriptionForm(request.POST, instance=profile) userform = UserForm(request.POST, instance=request.user) if appsettings.DEMO_SERVER and request.user.username == "demo": messages.warning(request, _("You can not change demo profile on the demo server.")) return redirect("profile") if form.is_valid() and userform.is_valid() and subscriptionform.is_valid(): # Save changes form.save() subscriptionform.save() userform.save() # Change language set_lang(request.user, request=request, user=request.user) # Redirect after saving (and possibly changing language) response = redirect("profile") # Set language cookie and activate new language (for message below) lang_code = profile.language response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) translation.activate(lang_code) messages.info(request, _("Your profile has been updated.")) return response else: form = ProfileForm(instance=profile) subscriptionform = SubscriptionForm(instance=profile) userform = UserForm(instance=request.user) social = request.user.social_auth.all() social_names = [assoc.provider for assoc in social] new_backends = [x for x in load_backends(BACKENDS).keys() if x == "email" or x not in social_names] response = render_to_response( "accounts/profile.html", RequestContext( request, { "form": form, "userform": userform, "subscriptionform": subscriptionform, "profile": profile, "title": _("User profile"), "licenses": Project.objects.exclude(license=""), "associated": social, "new_backends": new_backends, }, ), ) response.set_cookie(settings.LANGUAGE_COOKIE_NAME, profile.language) return response
def update_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = UserChForm(request.POST, request.FILES, instance=request.user.profile) # git = request.user.profile.github # print(git) # git = re.match(git, "https://github.com/") # print(git, 'it works!!!') # if git == match.group(0): # print(match.group(0), 'it works!') if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() # messages.success(request, _('Your profile was successfully updated!')) return redirect('accounts:user_list') else: user_form = UserForm(instance=request.user) profile_form = UserChForm(instance=request.user.profile) return render( request, 'user_update.html', { 'user_form': user_form, 'profile_form': profile_form, # 'user_obj': request.user })
def profile_edit(request): profile_ = Profile.objects.get( user=request.user ) # get profile qui a user(dans profile) = au user request if request.method == 'POST': userform = UserForm(request.POST, instance=request.user) profileform = ProfileForm( request.POST, request.FILES, instance=profile_) # request.FILES : pour uplaod file if userform.is_valid() and profileform.is_valid(): userform.save( ) # save user directement mais le profile nécessité la suivante myform = profileform.save( commit=False) # s'assurer que le profile est relié au user myform.user = request.user # s'assurer que le profile est relié au user myform.save() # save profile messages.success( request, 'updated') # qui sera affiché dans redirect template return redirect('accounts:profile') else: # (affichage) userform = UserForm( instance=request.user ) # request.user = data du user connecté (affichage) profileform = ProfileForm( instance=profile_ ) # profile_ = profile du user connecté (affichage) return render(request, 'accounts/profile/edit_profile.html', { 'userform': userform, 'profileform': profileform })
def profile_view(request): page_title = "Magnesium & Hope - Update your Profile" #todo- LIST IS JUST ALL treatment_list = Treatment.objects.filter( patient_id=request.user.patient.id) if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) patient_form = PatientForm(request.POST, instance=request.user.patient) if user_form.is_valid() and patient_form.is_valid(): user_form.save() patient_form.save() messages.success(request, ('Your profile was successfully updated!')) return redirect('haipumpfinder:profile') else: messages.error(request, ('Please correct the error below.')) else: u_p = request.user.patient u = request.user patient_form = PatientForm(request.POST or None, instance=u_p) user_form = UserForm(request.POST or None, instance=u) return render( request, 'haipumpfinder/profile.html', { 'user_form': user_form, 'patient_form': patient_form, 'treatment_list': treatment_list, 'page_title': page_title })
def edit_user(request): profile = User_Profile.objects.get(User_associated=request.user) user = User.objects.get(id=request.user.id) # img = None if request.method == "POST": profileForm = ProfileForm(request.POST, instance=profile, prefix="profile_form") userForm = UserForm(request.POST, instance=user, prefix="user_form") # passwordForm = PasswordChangeForm(data=request.POST, user=request.user, prefix="password_form") if profileForm.is_valid() and userForm.is_valid(): profileForm.save() userForm.save() # passwordForm.save() else: profileForm = ProfileForm(prefix="profile_form", instance=profile, initial={"Degree": profile.Degree, "Year_first_enrolled": profile.Year_first_enrolled,}) userForm = UserForm(prefix="user_form", instance=user, initial={'username': user.username, 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name}) return render_to_response("accounts/settings.html", {"profile_form": profileForm, "user_form": userForm}, context_instance=RequestContext(request))
def user_profile(request): """ Form to update User profile """ user_ = get_user_in_token(request) if request.method == 'POST': nic = request.POST.get('nicname') profileForm = UserProfileForm(request.POST, request.FILES, instance=user_.get_profile()) userForm = UserForm(request.POST, instance=user_) # if profileForm.is_valid() and userForm.is_valid(): if profileForm.is_valid(): profileForm.save() userForm.save() return output_format_json_response(201, message='프로필이 변경 되었습니다.', statusCode='0000') else: profile = request.user.get_profile() profile_data = { 'nickname': profile.nickname, 'picture_url': profile.get_image_url(), 'phone_number': profile.phone_number } return output_format_response(200, statusCode='0000', data=profile_data) return output_format_json_response(message='요청이 잘못되었습니다.', statusCode='5555')
def update_profile(request): """User can update its profile via Profile.html and related form""" if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = UserProfileForm(request.POST, instance=request.user.userprofile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, ('Your profile was successfully updated!'), extra_tags="Profile Updated") return redirect(reverse('index')) else: messages.error(request, ('Please correct the error below.')) return render( request, 'edit_profile.html', { "user_form": user_form, "profile_form": profile_form, "background_image": background["default"] }) else: user_form = UserForm(instance=request.user) profile_form = UserProfileForm(instance=request.user.userprofile) return render( request, 'profile.html', { 'user_form': user_form, 'profile_form': profile_form, "background_image": background["default"] })
def register_view(request): form = UserForm() if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): form.save() return HttpResponse('<h2>Thank you! You are now registered!</h2>') return render(request, 'accounts/registration_form.html', {'form': form})
def profile(request): user = request.user form = UserForm(instance=user) if request.method == 'POST': form = UserForm(request.POST, request.FILES, instance=user) if form.is_valid(): form.save() return render(request, 'accounts/profile.html', {'form': form})
def post(self, request): profile_form = UserForm(request.POST, request.FILES, instance=request.user) if profile_form.is_valid(): profile_form.save() return redirect('accounts_profile') else: return self._render(request, profile_form=profile_form)
def client_create(request): form = UserForm(request.POST or None) if request.method == "POST": if form.is_valid(): form.save() return redirect('accounts:clients') return render(request, "clients/client_create.html", context={"form": form})
def edit_profile(request): if request.method == 'POST': form = UserForm(request.POST, instance=request.user) if form.is_valid(): form.save() return redirect('account') else: form = UserForm(instance=request.user) return render(request, 'registration/edit_profile.html', {'form':form})
def editar_user(request): usuario = User.objects.get(id=request.user.id) if request.method == 'POST': form = UserForm(data=request.POST, instance=usuario) if form.is_valid(): form.save() return redirect('home') else: print(form.errors) else: form = UserForm(instance=usuario) return render(request, 'accounts/editar_user.html', locals())
def signup(request): """ 계정생성 """ if request.method == "POST": form = UserForm(request.POST) if form.is_valid(): form.save() return redirect('index') else: form = UserForm() return render(request, 'accounts/signup.html', {'form': form})
def sign_up(request): if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user_form.save() return render(request, 'accounts/sign_up_done.html', {'nickname': user_form['nickname'].value()}) else: print(user_form.errors) else: user_form = UserForm() return render(request, 'accounts/sign_up.html', {'user_form': user_form})
def add(request): user_form = UserForm(data=request.POST or None) if user_form.is_valid(): user_form.save(auth.get_user(request)) messages.success(request, u"Usuário salvo com sucesso.") return redirect('user_list') template_context = { 'user_form': user_form, } return render(request=request, template_name='add.html', dictionary=template_context)
def registerpage(request): form = UserForm() if request.method == "POST": form = UserForm(request.POST) if form.is_valid(): form.save() user = form.cleaned_data.get('username') messages.success(request, 'Account was created for ' + user) return redirect('login') context = {'form': form} return render(request, 'accounts/register.html', context)
def account_update(request, pk): if request.method == 'POST': form = UserForm(request.POST, instance=request.user) if form.is_valid(): form.save() messages.info(request, 'Wrong username or password.') return redirect('account_info') else: messages.info(request, 'Wrong username or password.') else: form = UserForm(instance=request.user) context = {"form": form} return render(request, 'accounts/account_update.html', context)
def register(request): if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user_form.save() return redirect('home_page') else: user_form = UserForm() context = {'user_form' : user_form} return render(request , 'registration/register.html' , context)
def edit_profile(request): if request.method == 'POST': # form = EditProfileForm(request.POST, instance=request.user) user_form = UserForm(request.POST, instance=request.user) if user_form.is_valid(): user_form.save() # form.save() return redirect(reverse('accounts:view_profile')) else: user_form = UserForm(instance=request.user) # profile_form = EditProfileForm(instance=request.user) # args = {'profile_form': profile_form, 'user_form': user_form} args = {'user_form': user_form} return render(request, 'accounts/edit_profile.html', args)
def organisation(request): if request.method == 'POST': form1 = UserForm(request.POST, request.FILES or None) print(form1.errors) if form1.is_valid(): form1.save() return redirect('home') else: form1 = UserForm() context = {'form1': form1} return render(request, 'accounts/organisation.html', context)
def user_profile(request): profile = request.user.get_profile() if request.method == "POST": # Read params form = ProfileForm(request.POST, instance=profile) subscriptionform = SubscriptionForm(request.POST, instance=profile) userform = UserForm(request.POST, instance=request.user) if form.is_valid() and userform.is_valid() and subscriptionform.is_valid(): # Save changes form.save() subscriptionform.save() userform.save() # Change language set_lang(request.user, request=request, user=request.user) # Redirect after saving (and possibly changing language) response = HttpResponseRedirect(reverse("profile")) # Set language cookie and activate new language (for message below) lang_code = profile.language response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) translation.activate(lang_code) messages.info(request, _("Your profile has been updated.")) return response else: form = ProfileForm(instance=profile) subscriptionform = SubscriptionForm(instance=profile) userform = UserForm(instance=request.user) response = render_to_response( "profile.html", RequestContext( request, { "form": form, "userform": userform, "subscriptionform": subscriptionform, "profile": profile, "title": _("User profile"), "licenses": Project.objects.exclude(license=""), }, ), ) response.set_cookie(settings.LANGUAGE_COOKIE_NAME, profile.language) return response
def client_update(request, pk): client = get_object_or_404(User, pk=pk) form = UserForm(request.POST or None, instance=client) if request.method == "POST": if form.is_valid(): form.save() messages.success(request, "Sucesso!") return redirect('accounts:clients') return render(request, "clients/client_update.html", context={ "form": form, "client": client })
def edit_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, files=request.FILES, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() return HttpResponseRedirect(reverse('accounts:profile')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) context = {'user_form': user_form, 'profile_form': profile_form} return render(request, 'accounts/edit_profile.html', context)
def profile_edit(request): user = request.user profile = user.profile form = ProfileForm(instance=profile) if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=profile) user_form = UserForm(request.POST, instance=user) if user_form.is_valid() and form.is_valid(): user_form.save() form.save() return redirect('user-profile') context = {'form': form, } return render(request, 'accounts/edit_profile.html', context)
def post(self, request, *args, **kwargs): # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and PublicForm. form_args = { 'data': request.POST, } user_form = UserForm(data=request.POST, instance=request.user) instance = UserProfile.objects.get(user=request.user) kwargs.setdefault('curruser', instance) profile_form = UserProfileForm(data=request.POST, instance=instance) # 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 sort out the Public 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 Public model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the Public model instance. profile.save() # 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 kwargs.setdefault("user_form", self.user_form_class(instance=instance.user)) kwargs.setdefault("profile_form", self.profile_form_class(instance=instance)) return super(Profile, self).get(request, *args, **kwargs)
def registration(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() 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, 'accounts/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def edit_my_profile(request): u = request.user try: profile = u.get_profile() except UserProfile.DoesNotExist: profile = None if request.method == 'POST': POST = request.POST.copy() POST['user'] = u.id profile_form = UserProfileForm(POST, request.FILES, instance=profile) user_form = UserForm(request.POST, request.FILES, instance=u) if user_form.is_valid() and profile_form.is_valid(): u = user_form.save() profile = profile_form.save() profile.user = u request.user.message_set.create(message="Your Profile was updated") return HttpResponseRedirect(profile.get_absolute_url()) else: user_form = UserForm(instance=u) if profile: profile_form = UserProfileForm(instance=profile) else: profile_form = UserProfileForm(initial={'user':request.user}) return render(request, 'edit_profile.html', {'profile_form':profile_form, 'user_form':user_form})
def register(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 profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() #Render the template depending on the context return render_to_response('register.html', {'user_form':user_form,'profile_form':profile_form, 'registered':registered}, context )
def register(request): 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() else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context = {'user_form' : user_form, 'profile_form' : profile_form} return render_to_response('accounts/register.html', context, context_instance=RequestContext(request))
def register(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 profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() #Render the template depending on the context return render_to_response( 'register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, context)
def signup_user(request): if request.method == 'GET': context = { 'user_form': UserForm(), 'profile_form': UserProfileForm(), } return render(request, 'accounts/signup.html', context) else: user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST, request.FILES) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() profile = profile_form.save(commit=False) profile.user = user group = Group.objects.get(name='customer') user.groups.add(group) profile.save() login(request, user) return redirect('user profile') context = { 'user_form': user_form, 'profile_form': profile_form, } return render(request, 'accounts/signup.html', context)
def register(request): if request.method == 'POST': u_form = UserForm(request.POST) p_form = ProfileForm(request.POST) if u_form.is_valid() and p_form.is_valid(): user = u_form.save() gender = p_form.cleaned_data.get('gender') date_of_birth = p_form.cleaned_data.get('date_of_birth') contact_number = p_form.cleaned_data.get('contact_number') author = AuthorProfile.objects.create( author=user, gender=gender, date_of_birth=date_of_birth, contact_number=contact_number) p_form = ProfileForm(request.POST, instance=author) p_form.save() messages.success(request, 'Your account has been created!') return redirect('login') else: u_form = UserForm() p_form = ProfileForm() context = {'u_form': u_form, 'p_form': p_form} return render(request, 'accounts/register.html', context)
def register(request): """ user registration form: GET/POST /registration """ 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(): human = True user = user_form.save(commit=False) user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() # save user in session login(request, user) logger.info('user {0} registered successfully'.format(user.username)) request.session['username'] = user.name return HttpResponseRedirect(reverse('profile')) else: print user_form.errors # raise Http404("Your registration is failed.") else: user_form = UserForm() profile_form = UserProfileForm() context = {'user_form': user_form, 'profile_form': profile_form}; return render(request, 'register.html', context);
def post(self, *args, **kwargs): data = self.request.POST # Load User and Profile forms form = UserForm(data) profileform = UserProfileForm(data) # Calls the validation method of the two model forms if form.is_valid() and profileform.is_valid(): # create and save user object user = form.save() # create and save profile object # assign `user` as a fk of the user field in the profile object profile = profileform.save(commit=False) profile.user = user profile.save() # authenticate user self.login_user_no_password(user) # Redirect to dashboard return HttpResponseRedirect(reverse('dashboard')) else: self.context['form'] = form self.context['profileform'] = profileform return render(self.request, self.template_name, self.context)
def accounts_add(request): error = "" if request.method == "POST": groups = request.POST.getlist('groups') email = request.POST.get('email') email = MyUser.objects.filter(email=email) form = UserForm(request.POST) if email: error = u'该账号已存在!' else: if form.is_valid(): user = form.save(commit=False) user.set_password(form.cleaned_data['password']) user.save() user.groups = Group.objects.get(id=1) return HttpResponseRedirect(reverse('user_user')) else: form = UserForm() groups = get_groups(request) print(groups) request.breadcrumbs( (('首页', '/'), ('用户列表', reverse('user_user')), ('添加用户', reverse('user_add')))) return render(request, 'accounts/user/user_add.html', { 'request': request, 'form': form, 'error': error, 'groups': groups })
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(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 'profile_pic' in request.FILES: print('found it') profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render( request, 'accounts/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): registered = False profile = UserProfile.objects.get(user=request.user) 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 profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'accounts/reg.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'profile': profile,} )
def register(request): # Like before, get the request's context. context = RequestContext(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_to_response( 'register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def add(request): user_form = UserForm( data=request.POST or None ) if user_form.is_valid(): user_form.save(auth.get_user(request)) messages.success(request, u"Usuário salvo com sucesso.") return redirect('user_list') template_context = { 'user_form': user_form, } return render( request=request, template_name='add.html', dictionary=template_context )
def register(request): if request.method == 'POST': uf = UserForm(request.POST) if uf.is_valid(): user = uf.save() return HttpResponseRedirect('main') else: uf = UserForm() return render_to_response('register.html', {'form': uf}, RequestContext(request))
def post(self, request, username=None, mode=None): context = {} if username is not None: return self.get(request, username, mode) context['detailsform'] = DetailsForm(instance=request.user.details) context['userform'] = UserForm(instance=request.user) context['settingsform'] = SettingsForm(instance=request.user.settings) if mode == "details": form = DetailsForm(request.POST, instance=request.user.details) if form.is_valid(): form.save() messages.success(request, "Persoonsgegevens veranderd") return self.get(request) else: messages.warning(request, "Er zijn een paar velden niet goed ingevuld") context['detailsform'] = form elif mode == "profile": user = UserForm(request.POST, instance=request.user) if user.is_valid(): user.save() messages.success(request, "Profiel opgeslagen!") return self.get(request) else: messages.warning(request, "Er zijn een paar velden niet goed ingevuld") context['userform'] = user elif mode == "settings": settings = SettingsForm(request.POST, request.FILES, instance=request.user.settings) if settings.is_valid(): settings.save() messages.success(request, "Intelling gewijzigd") return self.get(request) else: messages.warning(request, "Er zijn een paar velden niet goed ingevuld") context['settingsform'] = settings return render(request, "account/profile.html", context)
def update(request): if request.method == 'POST': userform = UserForm(request.POST, instance=request.user) profileform = UserProfileForm(request.POST, instance=request.user.get_profile()) if userform.is_valid() and profileform.is_valid(): userform.save() profileform.save() request.user.message_set.create(message="Ændringerne er blevet opdateret.") return HttpResponseRedirect(reverse('user_profile', args=[request.user.username])) else: userform = UserForm(instance=request.user) try: profileform = UserProfileForm(instance=request.user.get_profile()) except UserProfile.DoesNotExist: UserProfile(user=request.user).save() profileform = UserProfileForm(instance=request.user.get_profile()) return render_to_response( 'profiles/update.html', { 'userform': userform, 'profileform': profileform }, context_instance = RequestContext(request) )
def register(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.is_active = False username = user.username email = user.email salt = hashlib.sha1(str(random.random())).hexdigest()[:5] activation_key = hashlib.sha1(salt + email).hexdigest() key_expires = datetime.datetime.today() + datetime.timedelta(2) user.save() profile = profile_form.save(commit=False) profile.user = user profile.activation_key = activation_key profile.key_expires = key_expires profile.save() registered = True # Send email with activation key email_subject = 'Account confirmation' email_body = "Hey %s, thanks for signing up. To activate your account, click this link within \ 48hours http://127.0.0.1:8000/accounts/confirm/%s" % (username, activation_key) send_mail(email_subject, email_body, '*****@*****.**', [email], fail_silently=False) else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'accounts/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def create_account(request): """ *GET* Allows a user to create an account. *POST* Saves account with selected attributes *TEMPLATES* 'accounts/sign_up.html' """ from accounts.forms import UserForm if request.method == "GET": form = UserForm() redirect_to = "/login/#create" if "next" in request.GET: redirect_to = request.GET["next"] return render_with_user(request, "accounts/sign_up.html", {"form": form, "redirect_to": redirect_to}) if request.method == "POST": form = UserForm(request.POST) form_valid = False redirect_to = request.GET.get("next", "/login/#create") if form.is_valid(): # FORM IS VALID, CREATE USER form_valid = True form.save() return redirect(redirect_to) return render_with_user( request, "accounts/sign_up.html", {"form": form, "form_valid": form_valid, "redirect_to": redirect_to} )
def add_user(request): if request.user.is_authenticated and request.user.is_superuser: form = UserForm() if request.method == 'POST': form = UserForm(data=request.POST) username = request.POST['username'] email = request.POST['email'] if form.is_valid(): user = form.save(commit=False) return {'form':form} return render_to_response("home.html",{'404':'404'}, context_instance=RequestContext(request))
def signup_teacher(request): teacher_form = TeacherForm(request.POST or None) user_form = UserForm(request.POST or None) if request.method == "POST": if teacher_form.is_valid() and user_form.is_valid(): user = user_form.save() teacher = teacher_form.save(commit=False) teacher.user = user teacher.save() return redirect("index") return render(request, "signup_teacher.html", {"form": user_form, "teacher_form": teacher_form})
def registration(request): 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. 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 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, 'accounts/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def register(request): # Like before,get the request's context. context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # 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() # hash the password user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # save UserProfile model instance profile.save() registered = True # invalid forms, else: print user_form.errors, profile_form.errors # not a http post else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context return render_to_response( 'account/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context )
def signup_student(request): student_form = StudentForm(request.POST or None) user_form = UserForm(request.POST or None) if request.method == "POST": if student_form.is_valid() and user_form.is_valid(): user = user_form.save() student = student_form.save(commit=False) student.user = user student.save() return redirect("index") return render( request, "signup_student.html", {"form": user_form, "student_form": student_form, "grades": GRADE_CHOICES} )
def register(request): registered = False if request.method == 'POST': form = UserForm(data=request.POST) if form.is_valid(): user = form.save() user.set_password(user.password) user.save() registered = True messages.success(request, "Thank you for registering.") return redirect('login') else: form.errors else: form = UserForm() return render(request, 'accounts/register.html', {'form': form})
def register(request): if request.method == 'GET': user_form = UserForm() return render(request, 'accounts/login.html', {'user_form': user_form, }) elif request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() login_view(request) return HttpResponseRedirect(reverse_lazy('accounts:select_type')) else: return render(request, 'accounts/login.html', {'user_form': user_form, })
def register(request): user_form = UserForm(request.POST or None) if user_form.is_valid(): user = user_form.save() messages.success(request, u'Salvou o novo Usuário.') return redirect('home') else: messages.error(request, u'Deu Error visse') data = { 'form': user_form, } return render_to_response( 'registration/registration_form.html', data, context_instance=RequestContext(request) )
def register(request): template = 'accounts/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'))